Linux Regular Expression awk explanation, linux Regular Expression awk
Like sed, awk is a streaming editor. It also operates on the rows in the document and runs on one row. Awk is more powerful than sed. It can achieve what sed can do, and it can also do what sed cannot do. Awk is often used for segmentation;
Awk can be implemented without adding any parameters +? *. | These special symbols;
1. intercept a segment in the document
[root@yonglinux ~]# head -n2 passwd |awk -F: '{print $1}'rootbin[root@yonglinux ~]# head -n2 passwd |awk -F: '{print $0}'root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin[root@yonglinux ~]# head -n2 passwd |awk -F: '{print $1,$3,$7}'root 0 /bin/bashbin 1 /sbin/nologin
The-F option specifies the delimiter. If-F is not added, the Delimiter is a space or tab. Print is a Print action to Print a field. $1 is the first field, $2 is the second field, and so on. A special one is$0, which indicates the whole line.
Multiple Fields $1, $3, and $7 can be printed in {}, separated by commas;
The default delimiter for printing segments is space, which can be customized,Separators must be enclosed in double quotation marks.; You can also define the output Separator in OFS;
[root@localhost ~]# awk -F: '{print $3,$4}' 1.txt |head -50 01 12 23 44 7[root@localhost ~]# awk -F: '{print $3":"$4}' 1.txt |head -50:01:12:23:44:7[root@localhost ~]# awk -F: 'OFS="#"{print $3,$4}' 1.txt |head -50#01#12#23#44#7
[root@yonglinux ~]# head -n2 passwd |awk -F: '{print $1"#""@"$3"#"$7}'root#@0#/bin/bashbin#@1#/sbin/nologin
Pay attention to the awk format.-F is followed by single quotes and is then used as a separator. The print action must be enclosed by {}. Otherwise, an error is reported. Print can also print custom content, but the custom content should be enclosed in double quotation marks.
2. Match characters or strings
[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
A specific segment can be matched ,~ Indicates the meaning of the match. The first field is separated by a colon and then the keyword in // is matched;
[root@yonglinux ~]# awk -F: '/root/ {print $1,$3} /user/ {print $1,$3}' passwd root 0operator 11ftp 14saslauth 499user1 600
The awk can also be matched multiple times. In the above example, the full text matches the row containing the root keyword, then matches the row containing the user, and prints the matched 1st and 3 segments.
3. Conditional Operators
Determine if 3rd fields are 0
[
root@yonglinux ~]# awk -F: '$3=="0"' passwd root:x:0:0:root:/root:/bin/bash[root@yonglinux ~]# awk -F: '$3==10' passwd uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
Determine if 3rd fields are 10 and print the 7th fields of the row;
[root@yonglinux ~]# awk -F: '$3==10 {print $7}' passwd /sbin/nologin[root@yonglinux ~]# awk -F: '$3=="600"' passwd user1:x:600:501::/home/user1:/bin/bash
In awk, it can be determined by logical symbols. For example, '=' is equal to or can be understood as 'exact Match'. In addition, there are >,'> =, '<, '<= ,'! = And so on. It is worth noting that when comparing with a number, if we use double quotation marks to compare the number, awk will not regard it as a number but a character,It is considered a number without double quotation marks.
For example, double quotation marks are considered as characters, while single quotation marks and no quotation marks are considered as numbers;
[root@yonglinux ~]# awk -F: '$3>"500"' passwd | sort -t: -k 3 -n shutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinsshd:x:74:74:privilege-separated ssh:/var/empty/sshd:/sbin/nologindbus:x:81:81:system message bus:/:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinnobody:x:99:99:nobody:/:/sbin/nologinuser1: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>'500'' passwd | sort -t: -k 3 -n user1:x:600:501::/home/user1:/bin/bash
! = Is not matched, 7th fields are not equal to/sbin/nologin rows and must be enclosed in double quotation marks.
[root@yonglinux ~]# awk -F: '$7!="/sbin/nologin"' passwd root:x:0:0:root:/root:/bin/bashsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltuser1:x:600:501::/home/user1:/bin/bashmysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
In addition to logical comparison of characters in a segment, logical comparison can be performed between two segments.
For example, double quotation marks are used to show the number as a character;
[root@yonglinux ~]# awk -F: '$3>"5" && $3<"7"' passwd shutdown:x:6:0:shutdown:/sbin:/sbin/shutdownvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinuser1:x:600:501::/home/user1:/bin/bash
For example, the quotation marks are followed by numbers for comparison;
[root@yong ~]# awk -F: '$3>'5' && $3<'7' {print }' passwdshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
You can also use & "and" and | "or.
For example, print the rows whose segment 3rd is greater than segment 4th and segment 7th is/bin/bash;
[root@yonglinux ~]# awk -F: '$3>$4 && $7=="/bin/bash"' passwd user1:x:600:501::/home/user1:/bin/bash
For example, print the rows whose segment 3rd is less than 4th or whose segment 7th is/bin/bash;
[root@yonglinux ~]# awk -F: '$3<$4 || $7=="/bin/bash"' passwd root:x:0:0:root:/root:/bin/bashadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:ftp user:/var/ftp:/sbin/nologinuser1:x:600:501::/home/user1:/bin/bashmysql:x:27:27:mysql server:/var/lib/mysql:/bin/bash
4. built-in variables of awk
Common variables of awk include:
NF: How many segments are separated by delimiters
NR: number of rows
{Print NR ":" NF} lists the row numbers separated by colons to list the total number of segments;
[root@yonglinux ~]# head -5 passwd |awk -F: '{print NR":"NF}'1:72:73:74:75:7[root@yonglinux ~]# head -5 passwd |awk -F: '{print NF}'77777[root@yonglinux ~]# head -5 passwd |awk -F: '{print NR}'12345[root@yonglinux ~]# head -5 passwd |awk -F: '{print $NF}'/bin/bash/sbin/nologin/sbin/nologin/sbin/nologin/sbin/nologin
NF indicates the number of segments, while $ NF indicates the value of the last segment, while NR indicates the row number.
5. mathematical operations 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, the END should be noted that all rows have been executed. This is a special Syntax of awk. In fact, awk and sed can both be written into a script file and have their own syntax, in awk, if and for loops are acceptable.
Example: if, for example, if the value of the first segment is root, print the entire line;
[root@yonglinux ~]# awk -F: '{if ($1=="root") print $0}' passwd root:x:0:0:root:/root:/bin/bash
Example: for Loop, defines the sum variable, and the I value is the value of the 3rd segment; calculates the sum of the 3rd segment;
[root@yonglinux ~]# sum=0;for i in `awk -F: '{print $3}' passwd`;do sum=$[($sum+$i)];done;echo $sum1720
The above is all about the linux Regular Expression awk and I hope it will be helpful for your learning.