[Go]linux awk command

Source: Internet
Author: User
Tags processing text

Original link: http://blog.chinaunix.net/uid-23302288-id-3785105.html Awk is a line processor : Compared to the advantages of screen processing, there is no memory overflow or slow processing problem when processing large files, usually used to format text information awk process: each row is processed sequentially, then output awk command form:awk [-f|-f|-v] ' begin{}//{command1; Command2} end{} ' file[-f|-f|-v] large parameters,-f Specifies the delimiter,-F Invoke Script,- v defines the variable var=value' Referencecode blockBEGIN Initializes the code block, initializing the code, primarily referencing the global variable, setting the FS delimiter before processing each row//Match code block, can be a string or regular expression{} command code block, containing one or more commandsmultiple commands are delimited with semicolons.End code block, which executes after each line is processed, primarily for final calculation or output end summary information  Special points:$ A represents the entire current rowThe first field of each rowNF Field number variableNR record number per row, multi-file record incrementFNR is similar to NR, although multiple file records are not incremented, each file starts from 1\ t Tab\ n line breakdefining delimiters when FS beginRS  Enter the record delimiter, which defaults to a newline character (that is, the text is entered as one line)~ match, not accurate compared to = =!~ mismatch, inaccurate comparisons= = equals, must be all equal, exact comparison! = does not equal, exact comparison&& Logic and|| Logical OR+ matches 1 or more than 1/[0-9][0-9]+/two or two or more digits/[0-9][0-9]*/One or more numbersFileName file nameOFSoutput field delimiter, default is also a space, you can change to a tab, etc.The record delimiter for the ORS output, which defaults to a newline character, that is, the processing result is a line of output to the screen- f ' [: #/] ' defines three separators  Print & $Print is the primary command for awk to print the specified contentawk ' {print} '/etc/passwd = =awk ' {print $} '/etc/passwdawk ' {print ' "} '/etc/passwd//Do not output passwd content, but output the same number of blank lines, further explaining that Awk is a line of processing textawk ' {print ' a '} ' /etc/passwd//Output the same number of a line, a line only a letter Aawk-f ":" ' {print $} '/etc/passwd awk-f: ' {print $; print $ ' /etc/passwd // The first two fields of each row, branch output, further understanding a line of processing textawk-f: ' {print $1,$3,$6} ' ofs= ' \ t '/etc/passwd // output field 1,3,6, with TAB as delimiter - F Specify script fileawk-f Script.awk Filebegin{fs= ":"}{print $}//Effect vs .awk-f ":" ' {print '} ' is the same, except that the delimiter is specified in the code itself using FS awk ' begin{x=0}/^$/{x+=1} end{print "I find", X, "Blank Lines."} ' TestI find 4 blank lines.Ls-l|awk ' begin{sum=0}!/^d/{sum+=$5} end{print "Total size is", sum} '//Calculate file sizeTotal size is 17487  - F specify delimiter $ = After the specified delimiter, the first field, the third field, \ t is a tabone or more contiguous spaces or tabs are considered a delimiter, meaning that multiple spaces are considered a single spaceawk-f ":" ' {print $} '/etc/passwdawk-f ":" ' {print $ $} '/etc/passwd//The output is not separated from the $awk-f ":" ' {print $1,$3} '/etc/passwd//More a comma, $ $ and $ use a space-delimitedawk-f ":" ' {print $ ' "$/etc/passwd} 'manually add a space divider between//$1 and $awk-f ":" ' {print ' Username: "$" \t\t Uid: "$ $} '/etc/passwd//Custom Outputawk-f: ' {print NF} '/etc/passwd//show how many fields are in each lineawk- F:' {print $NF} '/etc/passwd//print the value of the NF field per lineawk-f: ' nf==4 {print} '/etc/passwd//show rows with only 4 fieldsawk-f: ' Nf>2{print $} '/etc/passwd//show rows with a number of fields greater than 2 per rowawk ' {print nr,$0} '/etc/passwd//output line number per lineawk-f: ' {print nr,nf, $NF, ' \ t ', ' $} '/etc/passwd//Print line number, number of fields, last field value, tab, contents per lineawk-f: ' Nr==5{print} '/etc/passwd//display line 5thawk-f: ' nr==5 | | Nr==6{print} '/etc/passwd//Show lines 5th and 6thRoute-n|awk ' Nr!=1{print} '//Do not display the 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 result awk '!/mysql/{print $} ' /ETC/PASSWD//output does not match MySQL lineawk '/mysql|mail/{print} '/etc/passwdawk '!/mysql|mail/{print} '/etc/passwdawk-f: '/mail/,/mysql/{print} '/etc/passwd//range matchingawk '/[2][7][7]*/{print $} '/etc/passwd//matches the line that contains 27 for the number beginning, such as 27,277,2777 ...awk-f: ' $1~/mail/{print $ '/etc/passwd//$1 matches the specified contentawk-f: ' {if ($1~/mail/) print '} '/etc/passwd//same as aboveawk-f: ' $1!~/mail/{print $ '/etc/passwd//mismatchawk-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//shorthandawk-f: ' {if ($1~/mail/) {print '} '/etc/passwd//Full Writeawk-f: ' {if ($1~/mail/) {print '} ' else {print $}} '/etc/passwd//if...else ...   Conditional Expressions = = = > >=awk-f ":" ' $1== ' MySQL ' {print $} '/etc/passwdawk-f ":" ' {if ($1== "MySQL") print $} '/etc/passwd//same as aboveAwk-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/p ASSWD//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/&& $3>8) print} '/etc/passwdawk-f: ' $1~/mail/| | $3>1000 {PRINT} '/etc/passwd//Logical ORawk-f: ' {if ($1~/mail/| | $3>1000) PRINT} '/etc/passwd  Numeric Operationsawk-f: ' $ > '/etc/passwdawk-f: ' $ > | | $ < 5 '/etc/passwdawk-f: ' $3+$4 >/etc/passwd 'awk-f: '/mysql|mail/{print $3+10} '/etc/passwd//third field plus 10 printingawk-f: '/mysql/{print $3-$4} '/etc/passwd//subtractionawk-f: '/mysql/{print $3*$4} '/etc/passwd//Productawk '/memfree/{print $2/1024} '/proc/meminfo//Divisionawk '/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 Use tab to split the field  output processing results to a file① output Route-n|awk ' Nr!=1{print > './fs '} ' in the command code block② using redirection for Output Route-n|awk ' Nr!=1{print} ' >./fs  formatted outputNetstat-anp|awk ' {printf '%-8s%-8s%-10s\n ", $1,$2,$3} 'printfrepresents the format output% formatted output delimiter-8 Length is 8 characterss denotes string typePrint the first three fields per line, specify the output string type (length 8), and the second field output string type(length is 8),The third field outputs a 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 1awk-f: ' {if ($3<100) next; else print} '/etc/passwd///Less than 100 skipped, otherwise displayedawk-f: ' Begin{i=1} {if (I<NF) print nr,nf,i++} '/etc/passwdawk-f: ' Begin{i=1} {if (I<NF) {print nr,nf} i++} '/etc/passwdanother formawk-f: ' {print ($3>100? "Yes": "No")} '/etc/passwdawk-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/cups D                   1     7913         &NB Sp                     1     10837       &NBS P                     1     9833       &NBSP ;                       1        Application 1awk-f: ' {print NF} ' helloworld.sh//output file How many fields per lineawk-f: ' {print $1,$2,$3,$4,$5} ' helloworld.sh//Output Top 5 fieldsawk-f: ' {print $1,$2,$3,$4,$5} ' ofs= ' \ t ' helloworld.sh//Output first 5 fields and use Tab delimited outputawk-f: ' {print nr,$1,$2,$3,$4,$5} ' ofs= ' \ t ' helloworld.sh//Tab delimited output Top 5 fields and print line numbers  Application 2awk-f ' [: #] ' {print NF} ' helloworld.sh//Specify multiple separators: #, output how many fields per lineawk-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 rowawk-f ' [: #/] ' {print $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, using KB as the unitLs-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 with a status of listen and ConnectNetstat-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 199 root 374What is the total size of the normal files for 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/10 "MB"} '  Application 7Output Score Tableawk ' 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
Related Article

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.