当前位置:网站首页>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
边栏推荐
- Some problems encountered in recent programming 2021 / 9 / 8
- C# Task. Delay and thread The difference between sleep
- Wiper component encapsulation
- Path environment variable
- Idea of batch manufacturing test data, with source code
- El date picker limits the selection range from the current time to two months ago
- VsCode-Go
- Node access to Alipay open platform sandbox to achieve payment function
- Mock test
- Shell script -- shell programming specification and variables
猜你喜欢
Bottom processing of stack memory in browser
. net type transfer
CentOS MySQL multi instance deployment
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
Scope and scope chain in JS
Mock test
Milvus 2.0 détails du système d'assurance de la qualité
线性代数感悟之1
文件操作《二》(5000字总结篇)
Change the password after installing MySQL in Linux
随机推荐
PHP高效读大文件处理数据
PHP efficiently reads large files and processes data
Bytevcharts visual chart library, I have everything you want
ClickHouse-数据类型
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
Kingdee Cloud Star API calling practice
SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
Bottom processing of stack memory in browser
PostgreSQL列存与行存
Zhimeng dedecms security setup Guide
freeCodeCamp----prob_ Calculator exercise
The new MySQL table has a self increasing ID of 20 bits. The reason is
拷贝构造函数 浅拷贝与深拷贝
[C#] 彻底搞明白深拷贝
Generate random numbers with high quality and Gaussian distribution
1-3 components and modules
1-5 nodejs commonjs specification
Regular filtering of Intranet addresses and segments
SPC introduction
Solution architect's small bag - 5 types of architecture diagrams