Shell Four Swordsman awk

Source: Internet
Author: User

(1) awk principle

The principle of awk is to process the data in a file line by row, find content that matches what is given on the command line, and then program the next step if a match is found. If no match is found, continue with the next line.


(2) Awk combat


<1> jfedu.txt file, find the JD.com line and print it on the screen (find with awk and SED)

Sed-n '/jd.com/p ' jfedu.txt awk '/jd.com/' jfedu.txt

Comments:

AWK has the same lookup function as SED, unlike when awk does not need to add a P parameter

<2> jfedu.txt file, find the line of JD.com and baidu.com, print on the screen (find with awk and SED)


Sed-n '/jd.com/,/baidu.com/p ' jfedu.txt awk '/jd.com/,/baidu.com/' jfedu.txt


<3> jfedu.txt file, print the first column and the second column

awk ' {print $1,$2} ' Jfedu.txt


<4> jfedu.txt files, such as written in this style (for example: 1:2:3:4:5), use awk to print out numbers (do not print out:)

Sed ' s/://g ' jfedu.txt | awk ' {print $1,$2,$3, $NF} '

Comments:

"$ $" means printing the first column, the third column, and the second column

$NF means the last column is printed


<5> jfedu.txt file, print first row and column

awk ' {print nr,$1} ' Jfedu.txt


<6> Print Jfedu.txt full content

awk ' {print $} ' Jfedu.txt


<7> View access.log log file, find Top 20 IP, arrange from small to large, remove duplicate IP

awk ' {print '} ' Access.log |  sort-nr|  uniq-c |  sort-nr| Head-20


<8> View Access.log log files, find 9:00-10:00, find Top 20 IP, arrange from small to large, remove duplicate IP

awk '/9:00/,/10:00/' Access.log | awk ' {print '} ' Access.log |  sort-nr|  uniq-c |  sort-nr| Head-20


<9> viewing disk partitions with DF-H, requiring: To view only content that is greater than or equal to the second row, the first column

df-h | awk ' nr>=2 {print '} '


<10> Print the first column of the passwd password file and append the contents of the output to the Name.csv file

Awk-f: ' {print $} '/etc/passwd >name.csv


<11> awk cuts with a colon, prints the first and last columns of the passwd file, but shows only the third and fifth rows

Awk-f: ' nr==3,nr==5 {print '} '/etc/passwd


<12> awk Specifies the sum of the first column in the file Jfedu.txt file

Cat jfedu.txt| awk ' {sum+=$1}end {print sum} '


<13> awk NR Line number in addition to 2 to 0, skip this line, continue to the next line, print on the screen

Awk-f: ' nr%2==0 {next} {print NR, $ '/etc/passwd


<14> awk NR Line number in addition to 2 to 1, skip this line, continue to the next line, print on the screen

Awk-f: ' Nr%2==1 {next} {print NR, $ '/etc/passwd


<15> awk Analysis Nginx Log status code is 404,502, such as Error page, print out the page open time, the user access IP, user access to the address of the page, and the IP from small to large to sort, to view the row in front

20 of IP

Awk-f: ' $9~/404|301|502|/{print $1,$7,$9, $NF} ' Access.log | sort-nr|  uniq-c | Sort-nr | head-20|

Shell Four Swordsman awk

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.