awk, like SED, is a streaming editor that operates on lines in a document, one line at a while. Awk is more powerful than SED, and it can do what sed can do, as well as what SED cannot do. Awk is often used to segment;
Awk does not need to add any parameters to implement +? * . | these special symbols;
1, intercept a paragraph in the document
[Root@yonglinux ~]# head-n2 passwd |awk-f: ' {print $} '
root
bin
[Root@yonglinux ~]# head-n2 passwd] |awk-f : ' {print $} '
Root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[Root@yonglinux ~]# HEAD-N2 passwd |awk-f: ' {print $1,$3,$7} '
root 0/bin/bash
bin 1/sbin/nologin
The-f option is to specify a delimiter, or a space or TAB key if not specified with-F. Print is a printed action that prints out a field. As the first field, the second field, and so on, one special is $, which represents the entire line .
{} can print multiple fields $1,$3,$7 print 1th, 3, 7, separated by commas;
The print segment default delimiter is a space, you can customize the separator, the delimiter needs to be enclosed in double quotes , or you can ofs define the output separator;
[Root@localhost ~]# awk-f: ' {print $3,$4} ' 1.txt |head-5
0 0
1 1 2 2 3 4 4
7
[root@localhost ~]# Awk-f: ' {print $ ': ' $} ' 1.txt |head-5
0:0
1:1
2:2
3:4
4:7
[root@localhost ~]# awk-f: ' OFS = "#" {print $3,$4} ' 1.txt |head-5
0#0
1#1
2#2
3#4
4#7
[Root@yonglinux ~]# head-n2 passwd |awk-f: ' {print ' # ' @ ' $ ' # ' $} '
Root#@0#/bin/bash
bin#@1#/sbin/ Nologin
Note The format of awk,---F immediately followed by single quotes, and then inside the separator, print action to use {} enclosed, otherwise it will be an error. Print can also print custom content, but custom content is enclosed in double quotes.
2, matching character or string
[Root@yonglinux ~]# awk-f: ' $1~/me/' passwd
games:x:12:100:games:/usr/games:/sbin/nologin
[ Root@yonglinux ~]# awk-f: ' $1~/user/' passwd
user1:x:600:501::/home/user1:/bin/bash
Can have a segment to match, ~ to indicate the meaning of a match, a colon to separate the first field and then match///in the keyword;
[Root@yonglinux ~]# awk-f: '/root/{print $1,$3}/user/{print $1,$3} ' passwd
root 0
operator-one
ftp
Aslauth 499
User1 600
Awk can also match more than once, as the full text of the previous example matches the line containing the root keyword, and then matches the row containing the user, printing the 1th and 3 paragraphs that match.
3. Conditional operator
To determine that the 3rd field is 0
[
Root@yonglinux ~]# awk-f: ' $3== ' 0 ' passwd root:x:0:0:root:/root:/bin/bash
[root@yonglinux ~]# awk-f: ' $3== Ten ' passwd
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
Judge the 3rd field to be 10 and print the 7th field of the row;
[Root@yonglinux ~]# awk-f: ' $3==10 {print $} ' passwd
/sbin/nologin
[root@yonglinux ~]# awk-f: ' $3== '] ' pas SWD
User1:x:600:501::/home/user1:/bin/bash
In awk, it can be judged by logical notation, for example, ' = = ' is equal to, can also be understood as ' exact match ' also have, ' >=, ' ' <=, '!=, and so on, it is noteworthy, in comparison with the number, if the comparison of the number of double quotes after the Then awk does not think of numbers, but as characters, and no double quotes are considered numbers.
example, double quotes are considered characters, plus single quotes and not added as numbers;
[Root@yonglinux ~]# awk-f: ' $3> ' passwd | Sort-t:-K 3-n
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x : 8:12:mail:/var/spool/mail:/sbin/nologin
vcsa:x:69:69:virtual console memory Owner:/dev:/sbin/nologin
sshd:x:74:74:privilege-separated ssh:/var/empty/sshd:/sbin/nologin
Dbus:x:81:81:system message bus:/:/sbin/ Nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
nobody:x:99:99:nobody:/:/sbin/nologin
User1:x:600:501::/home/user1:/bin/bash
[Root@yonglinux ~]# awk-f: ' $3>500 ' passwd | Sort-t:-K 3-n
user1:x:600:501::/home/user1:/bin/bash
[root@yonglinux ~]# awk-f: ' $3> '] passwd | sort-t :-K 3-n
User1:x:600:501::/home/user1:/bin/bash
!= is not matched , the 7th field is not equal to the/sbin/nologin line and needs to be enclosed in double quotes.
[Root@yonglinux ~]# awk-f: ' $7!= '/sbin/nologin ' ' passwd root:x:0:0:root:/root:/bin/bash '
Sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
User1:x:600:501::/home/user1:/bin/bash
Mysql:x:27:27:mysql Server:/var/lib/mysql:/bin/bash
In addition to a logical comparison of the characters for one segment, you can logically compare two segments.
example, after adding double quotes, look at the number as a character;
[Root@yonglinux ~]# awk-f: ' $3> "5" && $3< "7" ' passwd
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
vcsa:x:69:69:virtual Console Memory Owner:/dev:/sbin/nologin
user1:x:600:501::/home/user1:/bin/bash
example, after adding single quotes for a numeric comparison;
[Root@yong ~]# awk-f: ' $3> ' 5 ' && $3< ' 7 ' {print} ' passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/ Shutdown
Alternatively, you can use the && "and" and | | "or" means.
example, print a row with the 3rd paragraph greater than the 4th paragraph and the 7th paragraph/bin/bash;
[Root@yonglinux ~]# awk-f: ' $3>$4 && $7== '/bin/bash ' passwd
user1:x:600:501::/home/user1:/bin/bash
example, print a line with the 3rd paragraph less than the 4th paragraph, or the 7th paragraph as/bin/bash;
[Root@yonglinux ~]# awk-f: ' $3<$4 | $7== "/bin/bash" ' passwd
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x : 4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
UUCP:X:10:14:UUCP :/var/spool/uucp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/ Var/gopher:/sbin/nologin
ftp:x:14:50:ftp user:/var/ftp:/sbin/nologin
user1:x:600:501::/home/user1:/bin/ Bash
mysql:x:27:27:mysql Server:/var/lib/mysql:/bin/bash
4. awk's built-in variables
the variables commonly used in awk are:
NF: separated by delimiters how many segments are there?
NR: Line Count
{print NR: "NF}" lists the line numbers, separated by colons, listing the total number of segments;
[Root@yonglinux ~]# head-5 passwd |awk-f: ' {print NR ': ' NF} ' 1:7 2:7 ' 3:7 4:7 ' 5:7
[ Root@yonglinux ~]# head-5 passwd |awk-f: ' {print NF} '
7
7
7
7
7
[Root@yonglinux ~]# head-5 P ASSWD |awk-f: ' {print NR} '
1
2
3
4
5
[root@yonglinux ~]# head-5 passwd |awk-f: ' {print $N F} '
/bin/bash
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
NF represents the number of segments, while $NF is the last, while NR is the line number.
5, the mathematical operation in awk
awk can also perform mathematical operations on the values of each segment:
[Root@yonglinux ~]# awk-f: ' {(tot=tot+$3)}; End {print tot}; ' passwd
1720
Here's the end to note that all the rows have been executed, this is the awk-specific syntax, in fact awk and SED can be written in a script file, and have their own unique syntax, in awk using the If judgment, the for loop is OK.
example, if judgment, such as the value of the first paragraph is root, print the entire line;
[Root@yonglinux ~]# awk-f: ' {if ($1== ' root ') print $} ' passwd
Root:x:0:0:root:/root:/bin/bash
example, for loop, define sum variable, value of I value 3rd, and sum of the 3rd paragraph;
[Root@yonglinux ~]# sum=0;for i in ' awk-f: ' {print $} ' passwd ';d o sum=$[($sum + $i)];d one;echo $sum
1720
This is all about the Linux regular expression awk, I hope to help you learn.