当前位置:网站首页>Use of shell awk command

Use of shell awk command

2022-04-23 17:06:00 Magic Flute love

awk

A powerful text analysis tool , Read the document line by line , Slice each line with a space as the default separator , The cut part is analyzed again .

Basic usage

awk [ Option parameters ] 'pattern1{action1}  pattern2{action2}...' filename

pattern: Express AWK What to look for in the data , It's a matching pattern 
action: A series of commands executed when a match is found 

Only match pattern Will be executed action; Match the pattern1 Is executed action1, Match the 2 Is executed action2.

Description of common option parameters

Option parameters function
-F Specify the input file separator
-v Assign a user-defined variable

Case practice

##  Data preparation 
[bd@localServer ~]$ sudo cp /etc/passwd ./
root:x:0:0:root:/root:/bin/bash

###  Search for passwd Document to root All lines at the beginning of the keyword , And output the 7 Column .
[bd@localServer ~]$ awk -F : '/^root/{print $7}' passwd 
/bin/bash

###  Search for passwd Document to root All lines at the beginning of the keyword , And output the 1 Column and the first 7 Column , In the middle to “,” Division of no. .
[bd@localServer ~]$ awk -F : '/^root/{print $1","$7}' passwd 
root,/bin/bash


###  Display only /etc/passwd The first and seventh columns of , Comma separated , And add column names before all rows user,shell Add on last line "test,/bin/shuai".
[bd@localServer ~]$ awk -F : 'BEGIN{print "user, shell"} {print $1","$7} END{print "dahaige,/bin/zuishuai"}' passwd
user, shell
root,/bin/bash
bin,/sbin/nologin
...
bd,/bin/bash
test,/bin/shuai

####  Be careful :BEGIN  Execute before all data read rows ;END  Execute after all data execution .

###  take passwd Users in files id Increase in numerical value 1 And the output 
[bd@localServer ~]$ awk -v i=1 -F : '{print $3+i}' passwd
1
2
3
4
...

awk Built in variables for

Variable explain
FILENAME file name
NR Number of records read
NF The number of fields in the browsing record ( After cutting , Number of columns )

Case practice

####  Statistics passwd file name , Line number of each line , Columns per row 
[bd@localServer ~]$ awk -F : '{print "filename:"  FILENAME ", linenumber:" NR  ",columns:" NF}' passwd 
filename:passwd, linenumber:1,columns:7
filename:passwd, linenumber:2,columns:7
filename:passwd, linenumber:3,columns:7
...

####  cutting IP
[bd@localServer ~]$ ifconfig eth0 | grep "inet addr" | awk -F : '{print $2}' | awk -F " " '{print $1}' 
192.168.1.102

####  Inquire about sed.txt Line number of the blank line 
[bd@localServer ~]$ awk '/^$/{print NR}' sed.txt 
5

版权声明
本文为[Magic Flute love]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231704450881.html