Regular expression awk notes

Source: Internet
Author: User

awk ' condition type 1 {Action 1} condition type 2{action 2} ... ' filename

NF total number of fields per row ($)

NR awk is dealing with data from a row

FS input field split character, default is space

RS input Line delimiter

OFS output Field delimiter

ORS output Line delimiter

FNR respective statistic lines

BEGIN indicates that there are no files behind , only demos.

-F Delimiter

Awk–v Setting Variables


Example: printing a user with an ID less than 10 in a passwd file

cat/etc/passwd | Awk-f ":" ' $3<10 {print ' \ t ' $ $} '

Figure out the total for everyone

[[email protected] ~]# Cat AA | awk ' Nr==1{print $1,$2,$3,$4, "Total"}nr>=2{total=$2+$3+$4;print $1,$2,$3,$4,total} '

Name 1st 2nd 3th Total

Vbird 23000 24000 25000 72000

Dmtsai 21000 20000 23000 64000

Bird2 43000 42000 41000 126000


[[email protected] ~]# awk-vtest= "test" ' Begin{print test} '

Test

Or

[[email protected] ~]# awk ' begin{test= ' test ';p rint test} '

Test

Separate multiple statements with semicolons;

Printf

650) this.width=650; "Width=" 345 "height=" 297 "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>

[Email protected] ~]# awk-f: ' $3>500{print $1,$3} '/etc/passwd

Nfsnobody 65534

WWW 501

MySQL 502

Nagios 503

AAA 504

Print out users with IDs greater than $ in the passwd file

[Email protected] ~]# awk-f: ' $7~ ' bash$ ' {print $1,$7} '/etc/passwd

Root/bin/bash

Chen/bin/bash

Nagios/bin/bash

print out the seventh column of users who end with bash

Awk-f: ' $7!~ ' bash$ ' {print$1,$7} '/etc/passwd take reverse

[Email protected] ~]# awk-f: '/^r/{print$1,$7} '/etc/passwd

Root/bin/bash

Rpc/sbin/nologin

Rtkit/sbin/nologin

Rpcuser/sbin/nologin

go to the line starting with R

[[email protected] ~]# awk-f: '/^r/,/^m/{print$1,$7} '/etc/passwd line beginning with first R to first m

[Email protected] ~]# awk-f: ' $3==0,$7~ ' bash$ ' {print $1,$7} '/etc/passwd

Root/bin/bash

Take Id=0 and seventh rows ending with bash

[Email protected] ~]# awk-f: ' $3==0,$7~ ' nologin ' {printf '%-10s%-10s%-20s\n ', $1,$3,$7} '/etc/passwd

Root 0/bin/bash

Bin 1/sbin/nologin

take the third column , =0 and seventh, to print The value of Nologin at the end of the $ $7 "%-10s%-10s%-20s\n" representation of how many strings are spaced

begin/end awk Execute once and before execution awk Executes once after execution

[Email protected] ~]# awk-f: 'begin{print "username ID Shell"}$3==0,$7~ "Nologin" {printf "%-10s%-10s%-20s\n", $1,$3,$7} '/etc/passwd

Username ID Shell

Root 0/bin/bash

Bin 1/sbin/nologin

three columns of headers are printed before AWK executes

[[email protected] ~]# awk-f: ' begin{print ' username ID shell '}$3==0,$7~ ' nologin ' {printf '%-10s%-10s%-20s\n ', $1,$3,$7} end{print "END of File"}'/etc/passwd

Username ID Shell

Root 0/bin/bash

Bin 1/sbin/nologin

End of File

Print a closing after AWK executes

[[email protected] ~]# awk-f: ' {if ($1== "root") print $, "this is admin"; else print$1, "no admin"} '/etc/passwd

Root this is admin

Bin No admin

Daemon No admin

ADM No admin

If statement displays $ isadmin if $1=root otherwise displays no admin

[[email protected] ~]# awk-f:-V sum=0 ' {if ($3>500) sum++}end{print sum} '/etc/passwd

5

The-v variable sum is assigned a value of 0 if the ID is greater than sum+1 displays the value of sum after execution completes

Netstat-ant | awk '/^tcp/{state[$NF]++}end{for (s in state) {print S,state[s]}} '

View TCP Status

[[email protected] ~]# awk ' {count[$1]++}end{for (IP in count) {print Ip,count[ip]}} '/home/webserver/nginx/logs/ Access.log

Statistics website for each IP Access Volume


Another: File Compare tool diff

Diff parameter usage

-B ignores whitespace in one line

-B or slightly blank line

-I ignores case differences

Usage: diff [-bbi] file 1 file 2 (can also be compared to catalog)

Regular expression awk notes

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.