Analysis of AWK usage

Source: Internet
Author: User

Analysis of AWK usage
Cut command limitations

Df-h | cut-d ""-f 5/etc/passwd

The cut command cannot correctly intercept the content with spaces as the separator.

AWK format

Awk 'condition 1 {Action 1} condition 2 {Action 2 }... 'Filename'

Condition (Pattern) [relational expressions are generally used as conditions]:

X> 10 determine whether variable x is greater than 10

X> = 10 is greater than or equal

X <= 10 is less than or equal

Action ):

Formatted output: printf, print [\ n will be automatically added]

Process control statement: if/

Percentage of truncated/partitioned data

Df-h | grep sda2 | awk '{print $5}' | cut-d %-f1

BEGIN

Awk 'in in {printf "This is a test Script \ n"} {print $2 "\ t" $5} 'student.txt # first run the command followed by BEGIN

Awk 'in in {FS = ":"} {print $1 "\ t" $3} '/etc/passwd # display the specified separator

Awk-F: '{print $1 "\ t" $3}'/etc/passwd # Same as before

END

Cat/etc/passwd | grep/bin/bash | awk-F: 'End {print "The END... "} {print $1" \ t "$3} '# print the END Content Before the command ends.

Relational operators

Cat student.txt | grep-v NAME | awk '$3> = 80 {print $2 }'

Awk '$3> = 80 {print $2}' student.txt | grep-v NAME # An error may occur.

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.