当前位置:网站首页>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
边栏推荐
- . net cross platform principle (Part I)
- Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
- Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
- Interface document yaml
- Milvus 2.0 détails du système d'assurance de la qualité
- Baidu Map Case - Zoom component, map scale component
- RTKLIB 2.4.3源码笔记
- Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
- First knowledge of go language
- Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
猜你喜欢
Zhongang Mining: Fluorite Flotation Process
Bottom processing of stack memory in browser
Detailed explanation of the penetration of network security in the shooting range
VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
CentOS MySQL multi instance deployment
[registration] tf54: engineer growth map and excellent R & D organization building
Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
Modify the test case name generated by DDT
Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
随机推荐
How vscode compares the similarities and differences between two files
Kingdee Cloud Star API calling practice
SQL database
Promise (II)
Talk about browser cache control
Milvus 2.0 質量保障系統詳解
websocket
Further optimize Baidu map data visualization
AIOT产业技术全景结构-数字化架构设计(8)
Scope and scope chain in JS
Zhimeng dedecms security setup Guide
On lambda powertools typescript
[PROJECT] small hat takeout (8)
Milvus 2.0 质量保障系统详解
扫码登录的原理你真的了解吗?
◰ GL shadow map core steps
Rtklib 2.4.3 source code Notes
Error in v-on handler: "typeerror: cannot read property 'resetfields' of undefined"
手写事件发布订阅框架
Document operation II (5000 word summary)