当前位置:网站首页>Shell -- text processing command

Shell -- text processing command

2022-06-29 22:49:00 Wan Li Gu Cheng

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 colon
  • cut -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
原网站

版权声明
本文为[Wan Li Gu Cheng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/180/202206291301392938.html