The advantage of awk for sed grep is segmentation, dividing the delimiter into a small segment, matching, replacing, manipulating, logical judging, comparing ... such as
Basic operations of AWK
[[email protected] ~]# awk-f ': ' {print $} ' 1.txt////-F Specify delimiter $ is the third paragraph
0
1
2
3
.....
Specify multiple segments
[[email protected] ~]# awk-f ': ' {print $3,$4,$1} ' 1.txt
0 0 Root
1 1 Bin
2 2 Daemon
3 4 ADM
4 7 LP
.....
Specifies that the specified delimiter is displayed
[[email protected] ~]# awk-f ":" ' ofs= ":" {print $3,$4,$1} ' 1.txt
0:0:root
1:1:bin
2:2:daemon
3:4:adm
......
Match specified characters
[Email protected] ~]# awk '/root/' 1.txt
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
Match multiple characters
[Email protected] ~]# awk '/root|user/' 1.txt
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
saslauth:x:499:76: "SASLAUTHD user":/var/empty/saslauth:/sbin/nologin
with special characters.
[Email protected] ~]# awk '/r*o/' 1.txt
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
...
Segment Matching
[[email protected] ~]# awk-f ': ' $1~/r*o/' 1.txt////search for the first line with R*o with a colon delimiter
Root:x:0:0:root:/root:/bin/bash
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
.....
Segment matches and displays only the specified rows
[[email protected] ~]# awk-f ': ' $1~/r*o/{print $} ' 1.txt///The third paragraph of the line showing the first paragraph R*o
0
2
6
11
....
Match two keywords simultaneously and specify the specified segment of the line now
[Email protected] ~]# awk-f ': ' $1~/r*o/{print $1,$3}; $1~/user/{print $1,$3} ' 1.txt
[[email protected] ~]# awk-f ': ' $1~/r*o|user/{print $1,$3} ' 1.txt
Root 0
Daemon 2
Shutdown 6
Operator 11
.....
//////////////////////////////////////////////////////////////////////////////////////////
Summary: awk-f Specifies the delimiter ': ' ofs= ': ' Specifies that the separator that displays the result matches the '/r*o/' specified filter condition {print $} displays the specified file for the filter result
Awk-f ': ' $1~/r*o/' is specified according to the paragraph to search ~ followed by keywords
| You can combine searches;
Shell Primer-awk-1