Linux awk command detailed 2

Source: Internet
Author: User
Tags logical operators processing text

awk is a row processor: Compared to the advantages of screen processing, there is no memory overflow or slow processing when processing large files, usually used to format text information The awk processing process:Each row is processed in turn, and then the output awk command form:awk [-f|-f|-v] ' begin{}//{command1; Command2} end{} ' file [-f|-f|-v] Large parameter,-F specify delimiter,-F call script,-v define variable Var=value '          Initialization code block with code block begin, before processing each row, the initialization code, mainly refers to the global variable, set the FS delimiter//Match code block, can be a string or regular expression {} command code block, containing one or more commands; Multiple commands use semicolons to separate end-ending blocks of code that are executed after each row is processed, primarily for final calculations or for output end-of-summary information Special points:$ A represents the entire current row of the first field per row NF Field quantity variable nr per row record number, multi-file record increment FNR is similar to NR, but multi-file records do not increment, each file starts from 1 \ T tab \ n line break FS begin when defining separators RSEnter the record delimiter, which defaults to a newline character (that is, the text is entered as one line)~ match, compared to = = not accurate comparison!~ mismatch, imprecise comparison = = equals, must all equal, exact comparison! = Not equal, exact comparison && logic with | | A logical OR + match represents 1 or more/[0-9][0-9]+/or more than two or more digits/[0-9][0-9]*/one or more digits filename filename ofs output field delimiter, default is also empty Grid, you can change the record delimiter for ORS output, such as tabs, by default, the line break, that is, the processing result is one line output to screen-f ' [: #/] ' defines three separators Print & $print  is the main command for awk to print the specified content awk ' {print} '  /etc/passwd   = =   awk ' {print $} '  /etc/passwd  awk ' {print ' "} '  /etc/passwd                                             //does not output passwd content, but outputs the same number of blank lines, Further explains that Awk is a line of processing text awk ' {print ' a '} '   /etc/passwd                                          //outputs the same number of a rows with only one a letter Awk-f ":" ' { Print $ '  /etc/passwd awk-f: ' {print $; print $ '    /etc/passwd                    //the first two fields of each row, branch output, further understanding one lineLine Processing text awk  -f: ' {print $1,$3,$6} ' ofs= ' \ t '/etc/passwd        //output field 1, 3,6, with tabs as separators   - F Specify script fileAwk-f Script.awk filebegin{fs= ":"}{print $//effect is the same as awk-f ":" ' {print '} ' except that the delimiter uses FS to specify awk in the code itself ' begin{x= 0}/^$/{X+=1} end{print "I find", X, "Blank Lines."} ' test I find 4 blank lines. Ls-l|awk ' begin{sum=0}!/^d/{sum+=$5} end{print "Total size is", sum} '//Calculate file size Total size is 17487 - F Specify delimiterAfter the specified delimiter, the first field, the third field, \ t is a tab one or more contiguous spaces or tabs as a delimiter, that is, multiple spaces as a space awk-f ":" ' {print $} '  /etc/passwdawk-f ":" ' { Print $ '  /etc/passwd                       //$1 and $ phase Even output, not delimited awk-f ":" ' {print $1,$3} '  /etc/passwd                     & nbsp One more comma, $ $ and $ $ use a space to separate awk-f ":" ' {print $ ' "$ $} '  /etc/passwd               &NBSP ; Manually add space between  //$1 and $ $ awk-f ":" ' {print ' Username: ' $ ' \t\t Uid: ' $ $} '/etc/passwd      /Custom Output  a Wk-f: ' {print NF} '/etc/passwd                           '     //show how many fields are in each line awk -f:  ' {print $NF} '/etc/passwd             &NBS P               //print out the value of the NF field per line  awk-f: ' nf==4 {print} '/etc/passwd       &NBSP              //Show lines with only 4 fields Awk-f: ' Nf>2{print $} '/etc/passwd     &N Bsp                 //show rows with more than 2 fields per line awk ' {print nr,$0} '/etc/passwd                                 //output line number of each line awk- F: ' {print nr,nf, $NF, ' \ t ', ' $ '/etc/passwd      //print line number, number of fields, last field value, tab, contents per line awk-f: ' Nr==5{print} '   &NBSP;/ETC/PASSWD                         //show 5th line awk-f: ' Nr==5 | | Nr==6{print} '  /etc/passwd       //shows lines 5th and 6th Route-n|awk ' Nr!=1{print} '       & nbsp                              //Do not show the first line &nbs P //Match code block //Pure character match!//pure character mismatch ~//field value matches!~//field value does not match ~/a1|a2/field value matches A1 or A2awk '/mysql/'/etc/passwdawk '/mysql/{print} '/etc/passwdawk '/mysql/{print $} '/etc/passwd//Three instructions as a result awk '!/mysql/{print $} '/etc/passwd//output does not match MySQL line awk '/mysql|mail/{print} '/etc/passwdawk '!/mysql|mail               /{print} '/etc/passwdawk-f: '/mail/,/mysql/{print} '/etc/passwd//Range matching awk '/[2][7][7]*/{print $} '/etc/passwd Matches a line containing 27 for a number, such as 27,277,2777...awk-f: ' $1~/mail/{print $ '/etc/passwd//$1 matches the specified content to show Awk-f: ' {if ($ 1~/mail/) print $ '/etc/passwd//Same as above awk-f: ' $1!~/mail/{print $} '/etc/passwd//mismatch awk-f: ' $1!~/mail|my Sql/{print '/etc/passwd If statement must be used in {}, and the comparison content is expanded by ().Awk-f: ' {if ($1~/mail/) print '} '/etc/passwd//shorthand awk-f: ' {if ($1~/mail/) {print $            }} '/etc/passwd//Full write Awk-f: ' {if ($1~/mail/) {print '} ' else {print $}} '/etc/passwd If...else ... Conditional Expressions = = = > >=Awk-f ":" ' $1== ' MySQL ' {print $} '/etc/passwd awk-f ': ' {if ($1== "MySQL") print $ "/etc/passwd//Same as above Awk-f"                      : "' $1!=" MySQL "{print $} '/etc/passwd//Not equal to Awk-f": "' $3>1000{print $} '/etc/passwd                            Greater than Awk-f ":" ' $3>=100{print $ $} '/etc/passwd//greater than or equal to Awk-f ": ' $3<1{print $} '/etc/passwd Less than awk-f ":" ' $3<=1{print $ $} '/etc/passwd//less than equals logical Operators && | |Awk-f: ' $1~/mail/&& $3>8 {print} '/etc/passwd//logic with, ' match Mail ', and $3>8awk-f: ' {if ($1~/mail/&AMP;&A mp $3>8) Print} '/etc/passwdawk-f: ' $1~/mail/| | $3>1000 {print} '/etc/passwd//logic or awk-f: ' {if ($1~/mail/| | $3>1000) PRINT} '/etc/passwd Numeric OperationsAwk-f: ' $ > '/etc/passwd awk-f: ' $ > 100 | |                    $ < 5 '/etc/passwd awk-f: ' $3+$4 >/etc/passwdawk-f: '/mysql|mail/{print $3+10} '/etc/passwd Third field plus 10 print awk-f: '/mysql/{print $3-$4} '/etc/passwd//subtraction awk-f: '/mysql/{print $3*$4} '/ETC/PASSWD//Product awk '/memfree/{print $2/1024} '/proc/meminfo//Division awk '/mem Free/{print int ($2/1024)} '/proc/meminfo//rounding Output delimiter ofsawk ' $6 ~/fin/| | nr==1 {print nr,$4,$5,$6} ' ofs= "\ T" Netstat.txtawk ' $6 ~/wait/| | nr==1 {print nr,$4,$5,$6} ' ofs= ' \ t ' netstat.txt//Output field 6 matches the row of wait, where the output line number per line, field 4,5,6, and tab-delimited fields output processing results to a file① output Route-n|awk ' Nr!=1{print > './fs '} ' ② use redirection for Output Route-n|awk ' Nr!=1{print} ' >./fs formatted outputNetstat-anp|awk ' {printf '%-8s%-8s%-10s\n ", $1,$2,$3} ' printf represents a format output% formatted output delimiter-8 length of 8 characters s means the string type prints the first three fields per line, Specifies the first field output string type (length 8), the second field output string type (length 8), the third field output string type (length is ten) Netstat-anp|awk ' $6== "LISTEN" | | nr==1 {printf "%-10s%-10s%-10s \ n", $1,$2,$3} ' Netstat-anp|awk ' $6== ' LISTEN ' | | nr==1 {printf "%-3s%-10s%-10s%-10s \ n", nr,$1,$2,$3} ' If statementAwk-f: ' {if ($3>100) print "large"; else print "small"} '/etc/passwdsmallsmallsmalllargesmallsmallawk-f: ' begin{a=0; B=0} {if ($3>100) {a++; print "large"} else {b++; print "Small"}} end{print A, "\ T", B} '/ETC/PASSWD&NBSP;&NBSP;&NBSP;&NB sp;                                                                                                                    //id is greater than 100,a plus 1, otherwise b plus 1awk-f: ' {if ($3<100) next; else print} '/etc/passwd                       //Less than 100 skipped, otherwise awk-f: ' Begin{i=1} {if (I&LT;NF) print nr,nf, i++} '/etc/passwd   awk-f: ' Begin{i=1} {if (I&LT;NF) {print nr,nf} i++} '/etc/passwd another form awk-f: ' {print ($3&G T;100? "Yes": "No")} '  /etc/passwd awk-f: ' {print ($3>100 $ ": \tyes": $ $ ": \tno")} '  /etc/passwd  While statementAwk-f: ' Begin{i=1} {while (I&LT;NF) print NF, $i, i++} '/etc/passwd 7 root x 0 PNS 0 Root 57/root 6 ArrayNetstat-anp|awk ' nr!=1{a[$6]++} end{for (i in a) print I, "\ T", a[i]} ' Netstat-anp|awk ' nr!=1{a[$6]++} end{for (i in a) PRI ntf "%-20s%-10s%-5s \ n", I, "\ T", a[i]} ' 9523                               1     9929                   &NBSP ;           1     listen                 &NB Sp          6     7903                               1     3038/cupsd             &NBSP ;     1     7913                       &NBSP ;       1     10837                     &NBS P   &NBSP   1     9833                         &NBSP ;     1        Application 1Awk-f: ' {print NF} ' helloworld.sh//output file each line has a number of fields Awk-f: ' {print $1,$ 2,$3,$4,$5} ' helloworld.sh//Output Top 5 fields Awk-f: ' {print $1,$2,$3,$4,$5} ' ofs= ' \ t ' Helloworld.s H//Output the first 5 fields and use a tab-delimited output awk-f: ' {print nr,$1,$2,$3,$4,$5} ' ofs= ' \ t ' helloworld.sh//tab separates the first 5 fields of output and Print line number Application 2Awk-f ' [: #] ' {print NF} ' helloworld.sh//Specify multiple separators: #, output the number of fields per line Awk-f ' [: #] ' {print $1,$2,$3,$4,$5,$6,$7} ' ofs= ' \ t ' helloworld.sh//Tab delimited output multi-field Application 3Awk-f ' [: #/] ' {print NF} ' helloworld.sh//Specify three separators and output the number of fields per row awk-f ' [: #/] ' {p Rint $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12} ' helloworld.sh//tab-delimited output multi-field Application 4Calculate the size of the normal file in the/home directory, use KB as Unit Ls-l|awk ' begin{sum=0}!/^d/{sum+=$5} end{print "Total size is:", sum/1024, "KB"} ' Ls-l|awk ' Begin{sum=0}!/^d/{sum+=$5} end{print "Total size was:", int (sum/1024), "KB"} '//int is the meaning of rounding Application 5Count the number of connections that NETSTAT-ANP status is LISTEN and connect Netstat-anp|awk ' $6~/listen| connected/{sum[$6]++} end{for (i in sum) printf "%-10s%-6s%-3s \ n", I, "", Sum[i]} ' Application 6What is the total number of normal files for different users in the/home directory? Ls-l|awk ' Nr!=1 &&!/^d/{sum[$3]++} end{for (i in sum) printf "%-6s%-5s%-3s \ n", I, "", Sum[i]} ' MySQL 1 374 statistics the size of the normal files of different users in the/home directory? Ls-l|awk ' Nr!=1 &&!/^d/{sum[$3]+=$5} end{for (i in sum) printf "%-6s%-5s%-3s%-2s \ n", I, "", sum[i]/1024/1024, " MB "}" Application 7Output score Table awk ' begin{math=0;eng=0;com=0;printf ' Lineno.    Name No. Math 中文版 Computer total\n ";p rintf"------------------------------------------------------------\ n "}{math+=$3; e ng+=$4; com+=$5;printf "%-8s%-7s%-7s%-7s%-9s%-10s%-7s \ n", nr,$1,$2,$3,$4,$5,$3+$4+$5} end{printf "------------------------ ------------------------------------\ n ";p rintf"%-24s%-7s%-9s%-20s \ n "," Total: ", math,eng,com;printf"%-24s%-7s%- 9s%-20s \ n "," AVG: ", math/nr,eng/nr,com/nr} ' test0
[[email protected] home]# cat test0 Marry 2143 77Jack 2321, 45Tom 2122, 71Mike 2537, 95Bo B 2415 40 57 62

AWK Handbook
Http://www.chinaunix.net/old_jh/7/16985.html

Linux awk command detailed 2

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.