当前位置:网站首页>Shell -- text processing command
Shell -- text processing command
2022-06-29 22:49:00 【Wan Li Gu Cheng】
List of articles
Shell—— Text processing commands
1、cut
cut The command is used to display each line from the beginning num1 To num2 The characters of , Bytes and fields , And put these characters , Byte and field output .
Grammar format
cut [ Parameters ] file name
Parameters :
- -c : Split in characters , Back plus n It means which column to take , Such as -c 3.
- -d : Custom delimiter , The default separator is tab
“\t”
. - -f : Specify which column to extract .
Using examples
Space as separator , Intercept and output text cut.txt The first column of characters :
[[email protected] home]# cat cut.txt
jin mao
jing xian
ling xia
long cai
chi neng
lan chi
fen ji
[[email protected] home]# cut -d " " -f 1 cut.txt
jin
jing
ling
long
chi
lan
fen
Combined with the use of pipe symbols : Search for passwd file , Use colon as separator , Cut off the 1,6,7 Column
[[email protected] home]# cat /etc/passwd |grep bash$
root:x:0:0:root:/root:/bin/bash
mysql:x:997:1001::/home/mysql:/bin/bash
es:x:1001:1002::/home/es:/bin/bash
[[email protected] home]# cat /etc/passwd | grep bash$| cut -d ":" -f 1,6,7
root:/root:/bin/bash
mysql:/home/mysql:/bin/bash
es:/home/es:/bin/bash
Combined with the use of pipe symbols : Use colon as separator , Intercept all columns before the third colon :
cut -d ":" -f -3
: Intercept all columns before the third coloncut -d ":" -f 3-
: Intercept all columns after the third column
[[email protected] home]# cat /etc/passwd | grep bash$| cut -d ":" -f -3
root:x:0
mysql:x:997
es:x:1001
2、awk
awk The command is to scan the file line by line ( From 1 Line to last line ), Find the line that contains the target text , If the match is successful , Then the user's desired action will be performed on that line ; conversely , Do nothing with the line .
Grammar format
awk [ Parameters ] ' Script command ' file name
Parameters :
-F fs : Specified in fs As a separator for the input line ,awk The default separator for the command is a space or tab .
-f file : Read from script file awk Script instructions , Instead of entering instructions directly on the command line .
-v var=val : Before executing the process , Set a variable var, The initial value of the device is val.
Using examples
Use colon as separator , Search for passwd In file root All the lines at the beginning , And output the first and seven columns of the row , And separated by commas :
[[email protected] home]# cat /etc/passwd | awk -F ":" '/^root/{print $1","$7}'
root,/bin/bash
$0 For the entire text line
$n Represents the... In the text line n Data fields
BEGIN and AND keyword
BEGIN Will enforce awk Execute the script command specified after the keyword before reading data
END Will be in awk After reading the data, execute the script command specified after the keyword
awk 'BEGIN {print "USERNAME"} awk 'BEGIN {
print "PASSWORD"}
-v Use of custom variables
Put all users id Add the specified number i:
-v i=1: Defining variables i, The initial value is 1, So the variables in the script command i There is no need to modify , Modify outside i The value of the can
# Query all users id
[[email protected] home]# cat /etc/passwd | awk -v i=1 -F ":" '{print $3}'
0
1
2
3
4
5
6
7
997
1001
996
# All users id+1
[[email protected] home]# cat /etc/passwd | awk -v i=1 -F ":" '{print $3+i}'
1
2
3
4
5
6
7
8
998
1002
997
# All users id+5
[[email protected] home]# cat /etc/passwd | awk -v i=5 -F ":" '{print $3+i}'
5
6
7
8
9
10
11
12
1002
1006
1001
边栏推荐
- If you master these 28 charts, you will no longer be afraid to be asked about TCP knowledge during the interview
- 模板函数与特化函数实现高效dynamicCast
- Qdomdocument and qdomnode are used in QT to read XML
- Number theory - division and blocking
- Build a short video platform, fade in and fade out, and support left sliding and right pulley to broadcast pictures
- Qt5.14.2 error connecting to the MySQL database of Ubuntu 20.04
- Efficient implementation of dynamiccast with template function and specialization function
- Ansible automatic operation and maintenance
- MySQL backup database Linux
- wirehark数据分析与取证infiltration.pacapng
猜你喜欢
Why does copying files on a shared folder on a local area network (ERP server) result in the loss of the local Internet
The soft youth under the blessing of devcloud makes education "smart" in the cloud
触摸按键与按键控制对应的LED状态翻转
为什么在局域网(ERP服务器)共享文件夹上拷贝文件时导致全局域英特网断网
Still stay up late every day and work overtime to make statements? In fact, you don't know how to make reports efficiently
#第三天
Wireshark data analysis and forensics information pacapng
5-1 system vulnerability scanning
低代码、端到端,一小时构建IoT示例场景,声网发布灵隼物联网云平台
Deep parsing of kubernetes controller runtime
随机推荐
关于深度学习的概念理解(笔记)
VS2013如何让编写的程序在其它电脑上面也能运行
One click file sharing software jirafeau
Evaluation of powerful and excellent document management software: image management, book management and document management
利用kubernetes中的leader选举机制来完成自己的HA应用
股票开户安全吗?上海股票开户。
分布式消息中间件设计
Kubernetes architecture that novices must know
static关键字续、继承、重写、多态
缓冲流练习
工业细节都是钱和时间砸出来的
leetcode 416. Partition Equal Subset Sum 分割等和子集(中等)
Digital tracking analysis of insurance services in the first quarter of 2022
Day9 ---- 用户注册与登录
SYSTEMd debugging
#第三天
Steady! The best posture for thousands of microservices to access Zadig (helm chart)
啃下大骨头——排序(一)
【Proteus仿真】步进电机转速数码管显示
AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践