当前位置:网站首页>Shell script - "three swordsmen" awk command
Shell script - "three swordsmen" awk command
2022-07-28 06:57:00 【Fish fire loach】
shell Script ——“ Three swordsmen ” And awk command
One 、awk
1、 working principle
Read the text line by line , The default is space or TAB Key to separate the separator , Save the separated fields to the built-in variables , And execute the edit command according to the mode or condition .
sed Commands are often used for a whole line of processing , and awk They tend to divide a line into multiple “ Field ” And then deal with it .
awk Information is also read line by line , The execution result can be obtained through print The function of printing and displaying data fields .
In the use of AWK In the course of the order , You can use logical operators "&&“ Express " And ”、"||" Express “ or ”、“!” Express “ Not ”; You can also do simple mathematical operations , such as + - * / % ^, Means addition, subtraction, multiplication, division, remainder and power respectively
2、 Command format
awk Options ‘ Mode or condition { operation }’ file 1 file 2…
awk -f Script files file 1 file 2 …
3、awk Common built-in variables ( Can be directly used )

4、 Output text by line
awk ‘{print}’ testfile2 # Output everything
awk ‘{print $0}’ testfile2 # Output everything
awk ‘NR1,NR3{print}’ testfile2 # Output No 1~3 Row content
awk ‘(NR>=1)&&(NR<=3) {print}’ testfile2 # Output No 1~3 Row content
awk ‘NR1||NR3{print}’ testfile2 # Output No 1 That's ok 、 The first 3 Row content
awk ’ (NR%2)==1{print}’ testfile2 # Output the contents of all odd lines
awk ’ (NR%2)==0{print}’ testfile2 # Output the contents of all even lines
awk ‘/^root/{print}’ /etc/passwd # Output to root Beginning line
awk ‘/nologin$/{print}’ /etc/passwd # Output to nologin The line at the end
awk 'BEGIN {x=0};//bin/bashKaTeX parse error: Expected 'EOF', got '#' at position 40: …tc/passwd #̲ Statistics to /bin/bash At the end of the …" /etc/passwd
BEGIN Pattern representation , Before processing the specified text , You need to perform BEGIN Actions specified in the pattern , The command is executed only once ;
awk Then process the specified file , We'll do it later END Actions specified in the pattern ,END{} In the block , Often put in the print results and other statements .





5、 Output text by field ’:’
awk -F “:” ‘{print $3}’ /etc/ passwd # Output in each line ( Separated by spaces or tab stops ) Of the 3 A field
awk -F “:” ‘{print $1,$3}’ /etc/passwd # Output the 1、3 A field
awk -E “:” ‘$3<5{print $1,$3}’ /etc/passwd # Output No 3 Field values are less than 5 Of the 1、3 Field contents
awk -F “:” ‘!($3<200) {print}’ /etc/passwd # Output No 3 The value of each field is not less than 200 The line of
awk 'BEGIN {FS=”:"}; {if($3>=1000) {print}}’ /etc/passwd # I'll finish it first BEGIN The content of , And print out the contents of the text
awk -F “:” ‘{max=($3>=$4)?$3:$4;{print max}}’ /etc/ passwd
#($3>$4) ?$3:$4 Ternary operator , If the first 3 The value of the first field is greater than or equal to the 4 Values for fields , Then put the 3 The value of a field is assigned to max, Otherwise, No 4 The value of a field is assigned to max
awk -F “:” ‘{print NR,$0}’ /etc/passwd # Output each line content and line number , Every time a record is processed ,NR It's worth adding 1
awk -F “:” ‘$7~“/bash”{print $1}’ /etc/passwd # The output is colon delimited and the 7 Field (s) /bash Of the lines 1 A field
awk -F “:” ‘($1~“root”)&& (NF==7) {print $1,$2}’ /etc/passwd # Output No 1 Field (s) root And there are 7 The number of fields in the row 1、2 A field
awk -F “:” '($7!=”/bin/bash")&&($7!=“/sbin/nologin”) {print}’ /etc/passwd
# Output No 7 Fields are neither /bin/bash, Not for /sbin/nologin All of the line





6、 Through pipes 、 Double quotes call shell command
1、echo $PATHI awk ‘BEGIN{RS=“:”}; END{print NR}’ # Count the number of colon separated text paragraphs ,END{} In the block , Often put in the print results and other statements
2、awk -F: '/bashKaTeX parse error: Expected 'EOF', got '#' at position 37: …etc/passwd #̲ call wc -l Command statistics usage ba…" /etc/passwd
3、free -m | awk ’ /Mem:/ {print int ($3/ ($3+$4) *100)“%”}’ # View the current memory usage percentage
4、top -b -n 1 | grep Cpu | awk -F ‘,’ ‘{print $4}’ | awk ‘{print $1}’ # View the current CPU Idle rate ,(-b -n 1 It means that you only need 1 The output of times )
date -d “$(awk -F “.” ‘{print $1}’ /proc/uptime) second ago” +“&F %H:%M:%S”
# Display the last system restart time , Equate to uptime; second ago To show how many seconds ago ,+“%F %H: %M: %S" Equate to +”%Y-%m-%d %H: %M: %S" Time format of
awk ‘BEGIN {n=0 ; while ("w” 1 getline) n++ ; {print n-2}}’ # call w command , And used to count the number of online users
awk ‘BEGIN {“hostname” | getline ; {print $0}}’ # call hostname, And output the current host name
When getline There is no redirection between left and right “<” or “1” when ,awk The first First read the first - That's ok , Namely 1, then getline, Got it. 1 The second line below , Namely 2, because getline after ,awk Will change the corresponding NF,NR,FNR and $0 And so on , So at this time $0 The value of is no longer 1, It is 2 了 , Then print it out .
When getline There are redirections on the left and right “<” or “|” when ,getline Then function Directed input file , Since the file is just opened , Not by awk Read in - - That's ok , It's just getline Read in , that getline What is returned is the first line of the file , Instead of interlacing .
seq 10 | awk ‘{getline; print $0}’
seq 10 | awk ‘{print $0; getline}’
CPU Usage rate
cpu_us=top -b -n 1 | grep Cpu | awk '{print $2}
cpu_sy=top -b -n1 | grep Cpu | awk -F ',‘ ’{print $2}' | awk '{print $1}'
Cpu_sum=$ (( c p u u s + cpu_us+ cpuus+cpu_sy))
echo $cpu_ sum
echo “A B C D” | awk '{OFS=“|”;print $0;$1=$1;print $0} ’
A B C D
A|B|C|D
$1=$1 It's used to activate $0 The reassignment of , in other words
Field $1… And the number of fields NF A change in will prompt awk Recalculate $0 Value , It's usually changing OFS Then you need to output $0 Do this when you need to
OFS=“|”;print $0;$1=$1;print $0} ’
A B C D
A|B|C|D
$1=$1 It's used to activate $0 The reassignment of , in other words
Field $1… And the number of fields NF A change in will prompt awk Recalculate $0 Value , It's usually changing OFS Then you need to output $0 Do this when you need to
边栏推荐
- Technology sharing | common proxy tools for interface testing
- Prometheus monitoring Nacos
- 测试人生 | 二线城市年薪超40W?疫情之下涨薪100% + 是怎么做到的?
- OSI seven layer model
- ---Stack & queue---
- Technology sharing | sending requests using curl
- Wechat applet custom compilation mode
- Principle and configuration of NAT and pat
- Applet navigator cannot jump (debug)
- Software testing (concept)
猜你喜欢

Applet custom components - data, methods, and properties

It is recommended to wear air conduction earphones, which do not need to wear in ear

Applet creation component

Principle and configuration of NAT and pat

Which is the best air conduction Bluetooth headset? Air conduction Bluetooth headset ranking

raid磁盘阵列

技术分享 | 接口测试常用代理工具

Custom component -- data listener

Repair the faulty sector

思寒漫谈测试人职业发展
随机推荐
HDU-5805-NanoApe Loves Sequence(思维题)
How about air conduction Bluetooth headset? It's the most worthwhile air conduction headset to start with
进程和线程的区别
技术分享 | 使用postman发送请求
HDU-2036-改革春风吹满地(多边形面积模板)
Custom component -- data listener
How to store floating point data in memory
How to simulate the implementation of strcpy library functions
HDU-1097-A hard puzzle(快速幂)
搭建PHP7私有仓库
技术分享 | 如何模拟真实使用场景?mock 技术来帮你
DNS forward resolution experiment
ISO 3.0-server three power separation configuration
技术分享 | 接口自动化测试中,如何做断言验证?
单项链表的创建、遍历以及按要求查找结点
shell脚本——sort、uniq、tr、数组排序、cut、eval命令配置
C language memcpy library functions and the role of memmove
Network - transport layer (detailed version)
防火墙——iptables防火墙(四表五链、防火墙配置方法、匹配规则详解)
Ubuntu MySQL setting remote access permissions