当前位置:网站首页>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
边栏推荐
猜你喜欢
自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
Signalr can actively send data from the server to the client
Feign report 400 processing
CentOS MySQL multi instance deployment
Document operation II (5000 word summary)
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
groutine
Installing labellmg tutorial in Windows
Solution architect's small bag - 5 types of architecture diagrams
oracle 中快速获取表的列名列表
随机推荐
Mock test
Production environment——
Nodejs installation and environment configuration
Promise (I)
Redis docker installation
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey
Rtklib 2.4.3 source code Notes
ClickHouse-数据类型
Shell-sort命令的使用
Decimal format decimal / datetime conversion processing
文件操作《二》(5000字总结篇)
Shell script -- shell programming specification and variables
vscode如何比较两个文件的异同
Modify the test case name generated by DDT
Introduction to new functions of camtasia2022 software
Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
El cascade and El select click elsewhere to make the drop-down box disappear
Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
Grpc gateway based on Ocelot
Encapsulating the logging module