awk in Linux

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 ' Initializes the code block with code block begin, and initializes the code before processing each row, primarilyreferencing global variables, setting FS separatorsA matching code block, which can be a string or a regular expression {} command code block, containing one or more commands, and multiple commands that use semicolons to separate end-ending blocks of code that are executed after each row is processed, primarily for final calculation or output Summary information at the end 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 RS       Enter 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 | | Logical OR + matches represent 1 or more/[0-9][0-9]+/two or more than 1 digits/[0-9][0-9]*/one or more digits filename filename ofs output field delimiter , the default is also a space, you can change to a tab, such as ORS output of the record delimiter, the default is a newline character, that is, the processing result is a line of 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                           &N Bsp      //with a semicolon delimiter, enter the contents of the first field awk-f ":" ' {print $; Print $ '    /etc/passwd                   // The first two fields of each row, the branch output, further understand a line of processing text awk  -f ":" ' {print $1,$3,$6} ' ofs= "\ T"/etc/passwd          / /Output field 1,3,6, tab as delimiter   - 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 the value of the NF field per line  awk-f: ' nf==4 {print} '/etc/passwd   & nbsp   &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 | | Nr==6{print} '  /etc/passwd       //show lines 5th and 6th Route-n | awk ' NR!=1 {print} '                             &NBS P    //Do not show first line   //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|ma Il/{print} '/etc/passwdawk-f: '/mail/,/mysql/{print} '/etc/passwd//Interval match awk-f: '/[p][u][l]*/{print} '/etc/pas SWD//matches the line that contains the beginning of the Pul, including the Pul pull pulll, and so on. Awk-f: ' $1~/mail/{print $ '/etc/passwd//$1 matches the specified content to show Awk-f: ' {if ($1~/mail/) print '} '/etc/passwd//with above Same awk-f: ' $1!~/mail/{print $ '/etc/passwd//mismatch awk-f: ' $1!~/mail|mysql/{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 $ *$4} '/etc/passwd//Product awk '/memfree/{print $2/1024} '/proc/meminfo//Division Awk '/memfree/{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//id greater than 100,a plus 1, otherwise b plus 1a Wk-f: ' {if ($3<100) next; else print} '/etc/passwd///Less than 100 skipped, otherwise awk-f: ' Begin{i=1} {if (i<n F) 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>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) printf "%-20s%-10s%-5s \ n", I, "\ T", a[i]} ' 9523                     &NB Sp         1     9929                   &NBSP ;           1     listen                 &NB Sp          6     7903                               1     3038/cupsd             &NBSP ;       1     7913                     &NBSP ;         1     10837                   &NBS P        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.sh Output the first 5 fields and use tab-delimited output awk-f: ' {print nr,$1,$2,$3,$4,$5} ' ofs= ' \ t ' helloworld.sh//tab-delimited Output first 5 fields, and print the line number Application 2Awk-f ' [: #] ' {print NF} ' helloworld.sh//Specify multiple separators: #, output the number of fields per line Awk-f ' [: #] ' {p Rint $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 ' [: #/] ' {PR int $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12} ' helloworld.sh//tab-delimited output multi-field Application 4Calculates the size of the normal file in the/home directory, using KB as the 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 5Statistics NETSTAT-ANP the number of connections for the network 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 "} ' awk Handbook
http://www.chinaunix.net/old_jh/7/16985.html

awk in Linux

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.