Linux awk Command Usage

 
SUMMARY
awk is to read the file line by line, as the default delimiter spaces each row of slices, cut portions then various evaluation
awk workflow is such that: there reading '\ n' newline separated a record and the records in the specified field delimiter into domain, the domain is filled, then all domains $ 0, $ 1 represents the first field, $ n denotes the n-th field. The default field separator is a "key blank" or "[Tab] button"
 
awk command form:
awk [-F | -f | -v] 'the BEGIN {} // {Command1; Command2 the END {}}' File
 [-F | -f | -v] Great parameter, -F specify the delimiter, the script calls -f, -v define the variable value = var
'' reference block
BEGIN initialization code block prior to processing each row, initialization code, the main is a reference to a global variable, FS separator disposed
// matching block may be a string or a regular expression
{} command code block, comprising one or more commands
; multiple commands separated by semicolons
eND end of the block, on the code block after each line is processed again executed, mainly final calculation or output end of the summary information
 
special points:
$ 0 represents the entire current line
$ 1 per line first field
NF field number variable
NR record number for each row, and more file records increments
FNR and NR similar, but more documentation is not incremented each file from the beginning
\ t tab
\ n newline
defined by a separator FS BEGIN
record separator RS input, default line break (i.e., line by line, text input is based)
~ match is not accurate compared to the comparison ==
! ~ mismatched , inaccurate comparison
== equal, must be all equal, accurate comparison
! = not equal, accurate comparison
&& logical aND
|| or logic
represents one or more + 1 match
/ [0-9] [0-9] + / two or more digital
/ [0-9] [0-9] * / or one or more digital
fILENAME filename
OFS output field separator, default is spaces, tabs, etc. to
the recording output ORS separator, default to a newline character, i.e., the processing result is output to the screen line by line
-F '[: # /]' defines three separator
 
Print & $ 0
Print designated print content is mainly awk command
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 handling text line by line
awk '{print "a"} ' / etc / passwd // a same number of output lines, only one line of a letter
awk -F ":" '{}. 1 Print $' / etc / the passwd
awk -F: '$ {Print. 1; 2 Print $}' / etc / the passwd // the first two fields of each row, an output branch, a further understanding of the text line by line processing
awk -F: '{print $ 1 , $ 3, $ 6}' OFS = "\ t" / etc / passwd // output field 1, 3,6, and tabs as delimiter
 
-f specified script file
awk -f file script.awk
the BEGIN {
the FS = ":"
}
{} // Print effects. 1 $ awk -F ":" '{print $ 1} 'are 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}' // calculate the file size
Total size IS 17487
 
-F specified delimiter
$ 1 means after specifying the separator, the first field, the third field 3 $, \ tab T is
one or more consecutive spaces or tabs seen as a delimiter, i.e., a plurality of blank spaces seen
awk - F. ":" '{Print $. 1}' / etc / the passwd
awk -F ":" '{print $. 1 $ 3}' / etc / the 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 a space
awk -F ":"' {print $ 1 "" $ 3} '/ etc / passwd // manually add between $ 1 and $ 3 separated by spaces
awk -F ":" '{print "Username:" $ 1 "\ t \ t Uid:" $ 3}' / etc / passwd // custom output 
awk -F: '{print NF} '/ Etc / passwd // display how many fields 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 only 4 the field lines
awk -F: 'NF> 2 { print $ 0}' / etc / passwd // shows the number of fields per line is greater than 2
awk '{print NR, $ 0 }' / etc / passwd // output per line line number
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 NR == ==. 5. 6} {Print' / etc / // Show the passwd 5 and row line. 6
route -n | awk '! = NR. 1} {Print' is not the first line //
 
// matching block
! // // pure pure character matches the character does not match the field value matches ~ @ !   Field values do not match ~ @ ~ / a1 | a2 / a1 field value matches or A2
awk '/ MySQL /' / etc / the passwd
awk '/ MySQL / {Print}' / etc / the passwd
awk '/ MySQL / {Print $ 0}' / etc / the passwd // three instruction result as
awk '! / mysql / {print $ 0}' / etc / passwd // output line does not match the mysql
awk '/ mysql | mail / Print {}' / etc / the passwd
awk | / etc / the passwd '/ mysql mail / Print} {!'
awk -F: '/ mail /, / mysql / { print} '/ etc / passwd // matching section
awk' / [2] [7 ] [7] * / {print $ 0} '/ etc / passwd // matching comprises 27 to start with a digit line, such as 27,277, 2777 ...
awk -F: '$. 1 ~ / mail / {Print $. 1}' / etc / the passwd // $. 1 matches the specified contents only show
awk -F: '{if ($ 1 ~ / mail /) print $ 1}' / etc / passwd // same as above
awk -F: '! $ 1 ~ / mail / {print $ 1}' / etc / passwd // mismatch
awk -F: '$ 1 ~ /        mail | mysql / {print $ 1}!'/ etc / the passwd
 
the IF statement is
to be used in {}, and compare the contents with () in braces
awk -F: '{if ($ 1 ~ / mail /) print $ 1}' / etc / passwd // short
awk -F: '{if ($ 1 ~ / mail /) {print $ 1}}' / etc / passwd / / full write
awk -F: '{if ($ 1 ~ / mail /) {print $ 1} else {print $ 2}}' / etc / passwd // if ... else ...
 
 
conditional expression
== =>! > = 
awk -F ":" '$. 1 == "MySQL" {Print $. 3}' / etc / the passwd 
awk -F ":" '{IF ($. 1 == "MySQL") Print $. 3}' / etc / the 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 / the passwd
awk -F: '$ 1 ~ / mail / || $ 3> 1000 {print} '/ etc / passwd // logical or
awk -F:' {if ($ 1 ~ / mail / || $ 3> 1000) print} '/ etc / passwd
 
value calculation
awk -F:' $. 3> 100 '/ etc / the passwd   
awk -F:' $. 3> 100 || $. 3 <. 5 '/ etc / the passwd 
awk -F:' $. 3 + $. 4> 200 is '/ etc / the passwd
awk -F:' / MySQL | mail / {print $ 3 + 10} '/ etc / passwd // third field plus 10 printing
awk -F:' / mysql / { print $ 3- $ 4} '/ Etc / passwd // subtraction
awk -F: '/ mysql / { print $ 3 * $ 4}' / etc / passwd // find product
awk '/ MemFree / {print $ 2/1024}' / proc / meminfo // division
awk '/ MemFree / {print int ($ 2/1024)} ' / proc / meminfo // rounding
 
an output delimiter the OFS
awk' $. 6 ~ / the FIN / || NR ==. 1 {Print NR, $. 4, $. 5, $. 6} 'the OFS = "\ T" the netstat .txt
awk '$ 6 ~ / WAIT / || {Print. 1 == NR NR, $. 4, $. 5, 6} $' the OFS = "\ T" the Netstat.txt       
// matching WAIT line output field 6, wherein the output of each line number, 4,5,6 field, and tab-delimited fields using
 
outputs a processing result to a file
① command code in the output block directly -n route | '! = NR. 1 {Print> "./fs"}' awk  
② output redirection route -n | awk> ./fs' NR = 1 {print}! '
 
formatted output
netstat -anp | awk' {printf " % -8s% -8s% -10s \ n", $ 1, $ 2, $ 3} '
printf output format represents
% formatted output delimiter
-8 character length of 8
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)
-anp the netstat | awk '$. 6 == "the LISTEN" || NR. 1 == {the printf "% -10S% -10S -10S% \ n-", $. 1, $ 2, $}. 3'
the netstat -anp | awk '== $. 6 "the LISTEN" || NR. 1 == {the printf "% -3S -10S%%% -10S -10S \ n-", NR, $. 1, $ 2, $}. 3 '
 
the IF statement
awk -F:' {if ($ 3> 100 ) Print "Large"; the else Print "Small"} '/ etc / the passwd
Small
Small
Small
Large
Small
Small
awk -F:' the BEGIN {A = 0; B = 0} {IF ($. 3> 100) {A ++; Print " large "} else {B ++; print" small "}} END {print A," \ t ", B} '/ etc / the passwd
                                                                                                                  // ID is greater than 100, A plus 1, or B plus 1
awk -F: '{if($3<100) next; else print}' /etc/passwd                         //小于100跳过,否则显示
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
另一种形式
awk -F: '{print ($3>100 ? "yes":"no")}'  /etc/passwd
awk -F: '{print ($3>100 ? $3":\tyes":$3":\tno")}'  /etc/passwd
 
while语句
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
 
数组
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]}'
. 1 9523    
9929. 1    
the LISTEN. 6    
7903. 1    
3038 / the cupsd. 1    
7913. 1    
10837. 1    
9833. 1    
 
application. 1
awk -F: '{} of NF Print' helloworld.sh // output file number field per line
awk -F: '{print $ 1 , $ 2, $ 3, $ 4, $ 5} '5 before helloworld.sh // output fields
awk -F:' {print $ 1 , $ 2, $ 3, $ 4, $ 5} 'OFS =' \ t 'helloworld.sh // output and the first five fields separated by tabs output
awk -F: '{print NR, $ 1, $ 2, $ 3, $ 4, $ 5}' OFS = '\ t' helloworld.sh // Output 5 before tab-delimited fields, the line number and the print
 
application 2
awk -F '[: #]' '{ print NF}' helloworld.sh // specify multiple delimiters: #, output line number per field
awk -F '[: #]' '{print $ 1, $ 2, $ 3, $ 4, $ 5, $ 6, $ 7 } 'OFS =' \ t 'helloworld.sh // tab-delimited fields multiple output
 
applications. 3
awk -F' [: # /] '' of NF} {Print '// specified three helloworld.sh delimiters, and the output of each row number of fields
awk -F '[: # /] ' '{print $ 1, $ 2, $ 3, $ 4, $ 5, $ 6, $ 7, $ 8, $ 9, $ 10, $ 11, $ 12}' helloworld. sh // output multiplexer tab-delimited fields
 
applied 4
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 It means
 
application 5
statistical netstat -anp LISTEN state to the number of connections and the number cONNECT respectively
netstat -anp | awk '$ 6 ~ / LISTEN | cONNECTED / {sum [$ 6] ++} END {for (i in sum) printf " % -10s% -6s% -3s \ n ", i," ", sum [i]} '
 
applications 6
statistics / home directory files of the total number of ordinary users is much different?
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 size of the total size of an ordinary file under different user statistics / home directory 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"} '
 


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/zhangrui153169/p/11506517.html