Turn] linux awk command Detailed

Original link:  http://blog.chinaunix.net/uid-23302288-id-3785105.html

awk is a line processor : the advantages compared to screen processing, when dealing with large files do not appear out of memory or processing problem of slow, usually used to format the text information
awk process:  sequentially processing each line, and then output
awk command form:
awk [-F|-f|-v] ‘BEGIN{} //{command1; command2} END{}’ file
 [-F | -f | -v]    :  large parameter, -F specify the delimiter, -f calls the script, -v define the variable var = value
''      :       Reference block
The BEGIN   :   initialization code block prior to processing each row, initialization code, mainly global variable is used, the separator is provided FS
@      :        Matching block may be a string or a regular expression
{}      :        Command code block contains one or more commands
;      :       Multiple commands separated by semicolons
The END    :     the end of the block, each code block after the row processing executed again, is mainly calculated final digest information or the end of the output
 
Special points:
$ 0     :        represents the entire current line
$ 1      :       each line of the first field
Of NF     :       field the number of variables
NR     :       record number of each line, multi-file recording increment
FNR   :       and NR similar, but more documentation is not incremented each file from the beginning
\ t      :        tab
\ the n-      :       newline
The FS      :      the BEGIN defining delimiter
The RS :     input record separator, newline default (i.e., line by line, text input is based)    
~      :        Matching, the comparison is not precise as compared with ==
! ~     :        Do not match, inaccurate comparison
==    :       equal, must be all equal, accurate comparison
! =      :       Not equal, accurate comparison
&&  :      logical and
||      :         logical or
+    :          Match indicates at least one or a
/ [0-9] [0-9] + /   :  two or more digital
/ [0-9] [0-9] * /   :   one or more digital
FILENAME  : File name
The OFS    :    output field separator, the default is spaces, tabs, etc. to
The ORS    :      output record separator, newline default, i.e., the processing result is output to the screen line by line
-F '[: # /]'   :  definition of three delimiter
 
print & $0
Print  : is the main content of the specified print command awk
awk '{print}'  /etc/passwd   ==   awk '{print $0}'  /etc/passwd  
awk '{print ""}' / etc / passwd // passwd content does not output, but the output of the same number of blank lines, and is further explained awk process text line by line
awk '{print "a"}' / etc / passwd // a same number of output lines, only one line of a letter
awk -F":" '{print $1}'  /etc/passwd 
awk -F: '{print $ 1; print $ 2}' / etc / passwd // The first two fields of each row, an output branch is further understood that the processing line by line of text
awk -F: '{print $ 1, $ 3, $ 6}' OFS = "\ t" / etc / passwd // 1,3,6 output field, a tab as a delimiter
 
-f script file specified
awk -f script.awk  file
BEGIN{
FS=":"
}
{Print $ 1} // effects awk -F ":" '{print $ 1}' the same, except delimiter FS specified in the code itself
 
awk '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}' // File Size Calculation
total size is 17487
 
-F specify the delimiter
$ 1 means that the specified delimiter after the first field, the third field 3 $, \ T is a tab
One or more continuous spaces or tabs seen as a delimiter, i.e. seen as a plurality of spaces spaces
awk -F":" '{print $1}'  /etc/passwd
awk -F ":" '{print $ 1 $ 3}' / etc / passwd // $ 1 and $ 3 connected to the output, without the partition
awk -F ":" '{print $ 1, $ 3}' / etc / passwd // more than a comma, $ 1 and $ 3 separated by spaces
awk -F ":" '{print $ 1 "" $ 3}' / etc / passwd // $ 1 and $ 3 between the partition manually add a space
awk -F ":" '{print "Username:" $ 1 "\ t \ t Uid:" $ 3}' / etc / passwd // custom output  
awk -F: '{print NF}' / etc / passwd // number of fields displayed per line
awk -F: '{print $ NF}' / etc / passwd // the value of each field of lines printed NF
 awk -F: 'NF == 4 {print}' / etc / passwd // display line only four fields
awk -F: 'NF> 2 {print $ 0}' / etc / passwd // shows the number of lines per field is greater than the row 2
awk '{print NR, $ 0}' / etc / passwd // outputs row number of each row
awk -F: '{print NR, NF, $ NF, "\ t", $ 0}' / etc / passwd // print line order number, number of fields, field values ​​and finally, tabs, each line
awk -F: 'NR == 5 {print}' / etc / passwd // Display Line 5
awk -F: 'NR == 5 || NR == 6 {print}' / etc / passwd // display line 5 and line 6
route -n | awk '! NR = 1 {print}' // do not display the first row
 
// matching block
! // // pure pure character matches the characters do not match    ~ ~ // // field values match the field value does not match ~ / a1 |! A2 / a1 or a2 field values match   
awk '/mysql/' /etc/passwd
awk '/mysql/{print }' /etc/passwd
awk '/ mysql / {print $ 0}' / etc / passwd // results as three instructions
awk '! / mysql / {print $ 0}' etc / passwd // do not match output line mysql /
awk '/mysql|mail/{print}' /etc/passwd
awk '!/mysql|mail/{print}' /etc/passwd
awk -F: '/ mail /, / mysql / {print}' / etc / passwd // matching section
awk '/ [2] [7] [7] * / {print $ 0}' / etc / passwd // 27 comprises a matching number at the beginning of the line, such as 27,277,2777 ...
awk -F: '$ 1 ~ / mail / {print $ 1}' / etc / passwd // $ 1 it matches the specified display content
awk -F: '{if ($ 1 ~ / mail /) print $ 1}' / etc / passwd // same as above
awk -F: '! $ 1 ~ / mail / {print $ 1}' / etc / passwd // do not match
awk -F: '$1!~/mail|mysql/{print $1}' /etc/passwd        
 
IF statement
You must be in {}, and compare the contents with () in braces
awk -F: '{if ($ 1 ~ / mail /) print $ 1}' / etc / passwd // shorthand
awk -F: '{if($1~/mail/) {print $1}}'  /etc/passwd                                   //全写
awk -F: '{if($1~/mail/) {print $1} else {print $2}}' /etc/passwd            //if...else...
 
 
Conditional expression
==   !=   >   >=  
awk -F":" '$1=="mysql"{print $3}' /etc/passwd  
awk -F ":" '{if ($ 1 == "mysql") print $ 3}' / etc / passwd // same as above 
awk -F ":" '! $ 1 = "mysql" {print $ 3}' / etc / passwd // not equal
awk -F ":" '$ 3> 1000 {print $ 3}' / etc / passwd // greater than
awk -F ":" '$ 3> = 100 {print $ 3}' / etc / passwd // greater than or equal
awk -F ":" '$ 3 <1 {print $ 3}' / etc / passwd // less than
awk -F ":" '$ 3 <= 1 {print $ 3}' / etc / passwd // less
 
Logical Operators
&& || 
awk -F: '$ 1 ~ / mail / && $ 3> 8 {print}' / etc / passwd // logic, $ matching mail, and $ 3> 8
awk -F: '{if($1~/mail/ && $3>8) print }' /etc/passwd
awk -F: '$ 1 ~ / mail / || $ 3> 1000 {print}' / etc / passwd // logical OR
awk -F: '{if($1~/mail/ || $3>1000) print }' /etc/passwd 
 
Numerical computation
awk -F: '$3 > 100' /etc/passwd    
awk -F: '$3 > 100 || $3 < 5' /etc/passwd  
awk -F: '$3+$4 > 200' /etc/passwd
awk -F: '/ mysql | mail / {print $ 3 + 10}' / etc / passwd // print the third field plus 10 
awk -F: '/ mysql / {print $ 3- $ 4}' / etc / passwd // subtractor
awk -F: '/ mysql / {print $ 3 * $ 4}' / etc / passwd // find product
awk '/ MemFree / {print $ 2/1024}' / proc / meminfo // 除法
awk '/MemFree/{print int($2/1024)}' /proc/meminfo           //取整
 
An output delimiter OFS
awk '$6 ~ /FIN/ || NR==1 {print NR,$4,$5,$6}' OFS="\t" netstat.txt
awk '$6 ~ /WAIT/ || NR==1 {print NR,$4,$5,$6}' OFS="\t" netstat.txt        
// output matching WAIT field lines 6, wherein each of the output line number, field 4,5,6, and tab-delimited fields using
 
Output processing results to a file
① In the command code block is directly output route -n | '! NR = 1 {print> "./fs"}' awk   
② output redirection route -n | '! NR = 1 {print}' awk> ./fs
 
Formatted output
netstat -anp|awk '{printf "%-8s %-8s %-10s\n",$1,$2,$3}' 
printf output format representation
% Formatted output delimiter
-8 8 characters in length
s represents a string type
Printing each line of the first three fields, the first field specifies the type of output string (length 8), a second output string field type (length 8),
The third field of the output string type (length 10)
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 statement
awk -F: '{if($3>100) print "large"; else print "small"}' /etc/passwd
small
small
small
large
small
small
awk -F: 'BEGIN{A=0;B=0} {if($3>100) {A++; print "large"} else {B++; print "small"}} END{print A,"\t",B}' /etc/passwd 
                                                                                                                  // ID is greater than 100, A plus 1, or B plus 1
awk -F: '{if ($ 3 <100) next; else print}' / etc / passwd // skip less than 100, or display
awk -F: 'BEGIN{i=1} {if(i<NF) print NR,NF,i++ }' /etc/passwd   
awk -F: 'BEGIN{i=1} {if(i<NF) {print NR,NF} i++ }' /etc/passwd
Another form
awk -F: '{print ($3>100 ? "yes":"no")}'  /etc/passwd 
awk -F: '{print ($3>100 ? $3":\tyes":$3":\tno")}'  /etc/passwd
 
while statement
awk -F: 'BEGIN{i=1} {while(i<NF) print NF,$i,i++}' /etc/passwd 
7 root 1
7 x 2
7 0 3
7 0 4
7 root 5
7 /root 6
 
Array
netstat -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                               1     
9929                               1     
LISTEN                            6     
7903                               1     
3038/cupsd                   1     
7913                               1     
10837                             1     
9833                               1     
 
Application 1
awk -F: '{print NF}' helloworld.sh // output file number field per line
awk -F: '{print $ 1, $ 2, $ 3, $ 4, $ 5}' 5 Prev helloworld.sh // Output Field
awk -F: '{print $ 1, $ 2, $ 3, $ 4, $ 5}' OFS = '\ t' helloworld.sh front // outputs five fields separated by tabs and output
awk -F: '{print NR, $ 1, $ 2, $ 3, $ 4, $ 5}' OFS = '\ t' helloworld.sh // Output 5 before tab-delimited fields, print and the line number
 
Application 2
awk -F '[: #]' '{print NF}' helloworld.sh // specify multiple delimiters: #, each output line number field
awk -F '[: #]' '{print $ 1, $ 2, $ 3, $ 4, $ 5, $ 6, $ 7}' OFS = '\ t' helloworld.sh // tab-delimited fields output multiplexer
 
Application 3
awk -F '[: # /]' '{print NF}' helloworld.sh // specifies three separators, and outputs the number of fields per line
awk -F '[: # /]' '{print $ 1, $ 2, $ 3, $ 4, $ 5, $ 6, $ 7, $ 8, $ 9, $ 10, $ 11, $ 12}' helloworld.sh // tab-delimited output multi-field
 
Application 4
The calculation / home directory, the size of a regular file, the KB as a 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 is:", int (sum / 1024), "KB"}' // int is rounding the meaning of
 
Application 5
The number of connection statistics netstat -anp state to LISTEN and CONNECT is how much are
netstat -anp|awk '$6~/LISTEN|CONNECTED/{sum[$6]++} END{for (i in sum) printf "%-10s %-6s %-3s \n", i," ",sum[i]}'
 
Application 6
The total number of ordinary files under statistics / home directory of different users is how much?
ls -l|awk 'NR!=1 && !/^d/{sum[$3]++} END{for (i in sum) printf "%-6s %-5s %-3s \n",i," ",sum[i]}'   
mysql        199 
root           374 
The total size of the file size under ordinary statistics / home directory of different users is how much?
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 7
Output results table
awk 'BEGIN{math=0;eng=0;com=0;printf "Lineno.   Name    No.    Math   English   Computer    Total\n";printf "------------------------------------------------------------\n"}{math+=$3; eng+=$4; com+=$5;printf "%-8s %-7s %-7s %-7s %-9s %-10s %-7s \n",NR,$1,$2,$3,$4,$5,$3+$4+$5} END{printf "------------------------------------------------------------\n";printf "%-24s %-7s %-9s %-20s \n","Total:",math,eng,com;printf "%-24s %-7s %-9s %-20s \n","Avg:",math/NR,eng/NR,com/NR}' test0

[root@localhost home]# cat test0 
Marry   2143 78 84 77
Jack    2321 66 78 45
Tom     2122 48 77 71
Mike    2537 87 97 95
Bob     2415 40 57 62

Guess you like

Origin www.cnblogs.com/QaStudy/p/11514853.html