Grammar:
awk ' {command} ' filename multiple commands are separated by semicolons .
awk ' begin {Command1} {Command2} End{command3} ' Note: Begin, END needs to be capitalized
Common Variable Description:
FS: Specifies the delimiter, which is the default Space and tab. You can also abbreviate-F ";"
NR: Currently dealing with the "number of rows" of data, the row label in the file
NF: Number of columns per row (segment, field)
Example 1:
# with TAB as delimiter # print $0Indicates that all columns are printed, $1Prints the first column, does not write default print all columns # prints all columns, with-f Specifies the delimiter, $0awk-F"\ t" '{print $}'zss_test.txt# Print all columns, using the-F Specify delimiterawk-F"\ t" '{print}'zss_test.txt# Print all columns, specify the delimiter with FSawk 'fs== "\ t"; {Print $}'Zss_test.txt
Printing Results 1:
Example 2:
# #查看文件内容 Cat -n zss_test.txt# #NR, NF uses awk'fs== ' \ t '; {print $ \ t rows: "NR" \ T columns: "NF} ' zss_test.txt
Printing Results 2:
Example 3:
# #删除指定目录下的所有文件 with caution ls -l| awk ' {print "Rm-fr" $9} '| SH
Example 4:
# formatted output%s character with printf,%d integer,%f float; #%10s Right alignment, length 10, actual length greater than set length, by actual length #%-10s Align Left, other ditto # print file contentsCat-n kimbo_test.txt# Prints after the second line, column 1, column 4awk-F"|" 'Nr>=2{print $1,$4}'kimbo_test.txt# After printing the second line, column 1, column 4, output with printf format, column 1 left justifiedawk-F"|" 'nr>=2{printf "%-30s\t%5.2f\n", $1,$4;}'kimbo_test.txt# After printing the second line, column 1, column 4, output with printf format, column 1 right-justifiedawk-F"|" 'nr>=2{printf "%30s\t%5.2f\n", $1,$4;}'kimbo_test.txt# #多个命令, can be semicolon delimited # # after printing the second line, column 1, column 4, using the printf format session output, column 1 left justifiedawk-F"|" 'nr>=2{printf "%-30s\t", $ {}; nr>=2{printf "%5d\n", $4}'Kimbo_test.txt
Printing Results 4:
Example 5:
# #begin End usage # After printing the second line, column 1, column 4 awk -F"| " ' BEGIN {print "start-->"} Nr>=2{print $1,$4} end{print "END"} ' kimbo_test.txt
Printing Results 5:
Shell awk Command