The Linux regular expression awk explained

Source: Internet
Author: User
Tags gopher

awk, like SED, is a streaming editor that operates on lines in the document, one line at a line. awk is more powerful than SED, it can do what sed can do, and it can do the same with SED. awk is often used to segment;

Awk can implement + without adding any parameters?  * . | these special symbols;


1. Intercepting a segment in a document

[Email protected] ~]# head-n2 passwd |awk-f: ' {print $} ' rootbin[[email protected] ~]# head-n2 passwd |awk-f: ' {print +} ' root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin[[email protected] ~]# head-n2 passwd |awk-f: ' { Print $1,$3,$7} ' root 0/bin/bashbin 1/sbin/nologin

the function of the-f option is to specify a delimiter, or a space or tab as a separator if no-f is specified. Print is the printed action used to print out a field. $ $ for the first field, and $ for the second field, and so on, there is a special that is $ A , which represents the entire row .

You can print multiple fields within {} $1,$3,$7 printing 1th, 3, 7, separated by commas;


The print segment default delimiter is a space, you can customize the delimiter, the delimiter needs to be surrounded by colons , or you can ofs define the output delimiter ;

[Email protected] ~]# awk-f: ' {print $3,$4} ' 1.txt |head-50 (7[[email protected] ~]# awk-f: ' {print $ ': "$4 } ' 1.txt |head-50:01:12:23:44:7[[email protected] ~]# awk-f: ' ofs= ' # ' {print $3,$4} ' 1.txt |head-50#01#12#23#44#7

[Email protected] ~]# head-n2 passwd |awk-f: ' {print $ ' # ' @ ' $ $ ' # ' $7} ' Root#@0#/bin/bashbin#@1#/sbin/nologin

Note The format of awk, followed by the single quotation mark after-F, and then the delimiter inside, the print action to be enclosed in {}, otherwise it will be an error. Print can also print custom content, but the custom content is enclosed in double quotation marks.



2. Match characters or strings

[Email protected] ~]# awk-f: ' $1~/me/' passwd games:x:12:100:games:/usr/games:/sbin/nologin[[email protected] ~]# awk- F: ' $1~/user/' passwd user1:x:600:501::/home/user1:/bin/bash

You can have a segment to match, ~ to indicate the meaning of the match, to separate the first field with a colon and then match//the keywords;

[Email protected] ~]# awk-f: '/root/{print $1,$3}/user/{print $1,$3} ' passwd root 0operator 11ftp 14saslauth 499user1 600

Awk can also match multiple times, as the previous example full-text matches the row containing the root keyword, and then matches the row containing the user, printing the matching 1th, 3 paragraphs.



3. Conditional operator

Judging the 3rd field is 0

[Email protected] ~]# awk-f: ' $3== ' 0 "' passwd root:x:0:0:root:/root:/bin/bash[[email protected] ~]# awk-f: ' $3==10 ' pas SWD Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin


The 3rd field is judged to be 10 and the 7th field of the line is printed;

[Email protected] ~]# awk-f: ' $3==10 {print $7} ' passwd/sbin/nologin[[email protected] ~]# awk-f: ' $3== ' "' passwd u Ser1:x:600:501::/home/user1:/bin/bash


Awk can be judged by logical notation, such as ' = = ' is equal to, can also be understood as ' exact match ' In addition, ' >=, ' <, ' <=, '! ', etc., it is noteworthy that, when compared with the number, if the comparison of the number in double quotation marks, Then awk does not think of the numbers, but the characters, which are considered numbers without double quotes .


example, double quotation marks are considered to be characters, plus single quotes and no addition are considered numbers;

[[email protected] ~]# 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 
[Email protected] ~]# awk-f: ' $3>500 ' passwd | Sort-t:-K 3-n user1:x:600:501::/home/user1:/bin/bash[[email protected] ~]# awk-f: ' $3> ' passwd | Sort-t:-K 3-n User1:x:600:501::/home/user1:/bin/bash


! = is not a match , the 7th field is not equal to the/sbin/nologin line and needs to be enclosed in double quotation marks.

[Email protected] ~]# 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 a logical comparison of the characters for a segment, you can make a logical comparison between two segments.

[Email protected] ~]# 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


In addition, you can also use && and | | The meaning of "or".

example, prints the 3rd paragraph greater than the 4th paragraph, and the 7th paragraph is the/bin/bash line;

[Email protected] ~]# awk-f: ' $3>$4 && $7== '/bin/bash ' passwd user1:x:600:501::/home/user1:/bin/bash


example, prints the 3rd paragraph less than the 4th paragraph, or the 7th paragraph is the/bin/bash line;

[Email protected] ~]# 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. Awk built-in variables

The variables commonly used by AWK are:

NF: How many segments are separated by separators

NR: Number of rows

{Print NR ":" NF} lists line numbers, separated by colons, to list the total number of segments;

[Email protected] ~]# head-5 passwd |awk-f: ' {print NR ': ' NF} ' 1:72:73:74:75:7[[email protected] ~]# head-5 passwd |awk  -F: ' {print NF} ' 77777[[email protected] ~]# head-5 passwd |awk-f: ' {print NR} ' 12345[[email protected] ~]# head-5 passwd |awk-f: ' {print $NF} '/bin/bash/sbin/nologin/sbin/nologin/sbin/nologin/sbin/nologin
NF indicates how many segments, while $NF is the last, and NR is the line number.

5. Mathematical operations in awk
Awk can also perform mathematical operations on the values of individual segments:
[Email protected] ~]# awk-f: ' {(tot=tot+$3)}; END {print tot}; ' passwd 1720

The end of this is to note that all the rows have been executed, which is the syntax specific to awk, in fact awk, along with SED, can be written as a script file, with their own syntax, using if judgment in awk, and for loops is OK.


example, if judgment, such as the value of the first paragraph is root, the entire line is printed;

[[email protected] ~]# awk-f: ' {if ($1== "root") print $} ' passwd Root:x:0:0:root:/root:/bin/bash

example, for loop, defines the sum variable, I value is the value of the 3rd paragraph and the sum of the 3rd paragraph;

[[email protected] ~]# sum=0;for i in ' awk-f: ' {print $} ' passwd ';d o sum=$[($sum + $i)];d one;echo $sum 1720




This article is from the "Model Student's Learning blog" blog, please be sure to keep this source http://8802265.blog.51cto.com/8792265/1633022

The Linux regular expression awk explained

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.