Shell Practice-awk Command

Source: Internet
Author: User

Awk-f ': ' {print $3,$4} ' 1.txt

Awk-f ': ' ofs= ': ' {print $3,$4} ' 1.txt

awk '/root|user/' 1.txt matches root or user

awk '/(OO) +/' 1.txt one or more oo

Awk-f ': ' $1~/r*o/{print $} ' 1.txt the first paragraph to match r*o, and only print the third paragraph

Awk-f ': ' $1~/root/{print $1,$3}; $1~/user/{print $1,$3} ' 1.txt matches separately

Awk-f ': ' $1~/root|nobody/{print $1,$3} ' 1.txt or

Conditional operator

Awk-f ': ' $1== ' root ' {print '} ' 1.txt

Awk-f ': ' $1== ' root ' | | $7~/nologin/' 1.txt The first paragraph is root or 7th paragraph is nologin

You can use!~ to indicate that the mismatch $7!~/nologin/the 7th segment does not match the login

awk position variable nr line

Awk-f ': ' nr<10 {print $5,$1,$7} ' 1.txt printing the first 9 lines

Awk-f ': ' {if (nr==10) print $1,$3,$4} ' 1.txt

Awk-f ': ' ofs= "%" {if (nr==10) print $1,$2} ' passwd print paragraph 122 of line tenth

Awk-f ': ' {print NF} ' 1.txt prints a few paragraphs in each line

Awk-f ': ' ofs= ' # # ' {if (nf==7) print $1,$7} ' passwd printing is the first and seventh paragraphs in each row of paragraph 7

Awk-f ': ' {print $NR print $NF} ' 1.txt

Awk-f ': ' ofs= ': ' {$7=$3+$4 print $ '} 1.txt

Awk-f ': ' {(sum=sum+$3)}; END {print sum} ' 1.txt loop (note the position of the single quotation mark)

Awk-f: '/^root:/{ofs= ' # # ';p rint $1,$3,$6;} ' passwd


This article is from the "Share,open source" blog, so be sure to keep this source http://liqilong2010.blog.51cto.com/3029053/1857120

Shell Practice-awk Command

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.