Shell awk Command

Source: Internet
Author: User

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

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.