Some uses of Shell awk

Source: Internet
Author: User

#1, print the usage of the mounted directory, the default is separated by a space

df-ph | awk ' {print $5,$6} '


#2, separated by a space, colon, \ t, semicolon

Awk-f ' [: \ t;] ' ' {print '} '


#3, Print lines 3rd through 5th in the 6.txt file, nr represents the print line, and $ A is the text all fields

awk ' nr==3,nr==5 {print} ' 6.txtawk ' nr==3,nr==5 {print $} ' 6.txt

Column 2nd and 4th of line 3rd through 5th in #打印6. txt file

awk ' nr==3,nr==5 {print $2,$4} ' 6.txt

#打印7. txt file, line numbers and contents that are longer than 6

awk ' Length ($) >6 {print nr,$0} '

#以冒号切割, printing the first column shows only the first 5 rows:

Cat/etc/passwd|head-5|awk-f: ' {print '} ' awk-f: ' nr>=1&&nr<=5 {print '} '/etc/passwd

#NR行号除以2余数为0则跳过该行, proceed to the next line, print on the screen:

awk ' nr%2==0 {next} {print nr,$0} ' 7.txt


#4, reference shell variables, use-V or double quotation marks + single quotes:

Awk-v Str=hello ' {print STR, $NF} ' 7.txtstr= ' hello '; echo| awk ' {print ' ' ${str} ' ";} '


#5, the sum of the first column of the specified file Jfedu.txt:

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


To #6, add a custom character:

Ifconfig eth0|grep "Bcast" |awk ' {print ' IP_ ' $ $} ' awk-f: ' {print ' UserName: ' $ '/etc/passwd


#7, formatted output passwd content, printf print string,% formatted output delimiter, s denotes string type, 12 is 12 characters, 6 means 6 characters:

Awk-f: ' {printf '%-12s%-6s%-8s\n ', $1,$2, $NF} '/etc/passwd

#OFS输出格式化 \ t:

Netstat-an|awk ' $6 ~/listen/&&nr>=1&&nr<=10 {print nr,$4,$5,$6} ' ofs= ' \ t '


#8, awk and if combination combat, judge the number comparison

Echo 3 2 1 | awk ' {if (($1>$2) | | | ($1>$3)) {print $ {} else {print \ {}} '


Combination of #9, awk and array combat, statistics passwd file users

Awk-f: ' BEGIN {count=0;} {Name[count] = $1;count++;}; End{for (i = 0; i < NR; i++) print I, Name[i]} '/etc/passwd


#10, awk analysis of the Nginx access log status code 404, 502 and other error information page, the number of statistics greater than 20 IP address.

awk ' {if ($9~/502|499|500|503|404/) print $1,$9} ' Access.log|sort|uniq–c|sort–nr | awk ' {if ($1>20) print $ '

#11用 the ciphertext portion of the/etc/shadow file replaces the "x" position in the/etc/passwd, generating a new/tmp/passwd file.

awk ' begin{ofs=fs= ': '} nr==fnr{a[$1]=$2}nr>fnr{$2=a[$1];p rint >> "/tmp/passwd"} '/etc/shadow/etc/passwd

#12, awk count Server Status connections:

Netstat-an | awk '/tcp/{s[$NF]++} END {for (a in s) {print A,s[a]}} ' Netstat-an | awk '/tcp/{print $NF} ' | Sort | Uniq-c

#13, get the IP address of the eth0

Ifconfig eth0|grep "Bcast" |awk ' {print $} ' | Awk-f: ' {print $} '


#14, add output at the beginning and end of the output

#在所有行添加列名name, Shell, add "Blue,/bin/nosh" to the last line

Awk-f ': ' BEGIN {print ' Name,shell '} {print $ '---' $7} END {print ' Blue,/bin/nosh '} '/etc/passwd

#15, find out which system users are not allowed to log on

Awk-f: '/nologin/{print $ '/etc/passwd


#16, identify the user of the root group

Awk-f: ' $4 ==0 {print ' The user of group root: ' $ '/etc/passwd


This article is from the "Mo Jing" blog, reproduced please contact the author!

Some uses of Shell awk

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.