当前位置:网站首页>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
边栏推荐
猜你喜欢

Prometheus monitoring Nacos

Yapi vulnerability hanging horse program chongfu.sh processing

Centos7 deploy MySQL database server

Dynamic memory management function of C language

Network - transport layer (detailed version)

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

防火墙——iptables防火墙(四表五链、防火墙配置方法、匹配规则详解)

How to simulate the implementation of strcpy library functions

Common models in software development

Custom component -- pure data field & component life cycle
随机推荐
Ten thousand words summarize and realize the commonly used sorting and performance comparison
What is the most practical gift for Tanabata? A gift that will never go wrong is worth buying
Ubuntu18.04 set up redis cluster [learning notes]
DNS forward resolution experiment
技术分享 | 使用 cURL 发送请求
Applet navigator cannot jump (debug)
Detailed explanation of LNMP construction process
Test interview questions collection (I) | common required questions and procedures of software testing (with answers)
Custom component -- data listener
Technology sharing | send requests using postman
技术分享 | 使用postman发送请求
shell脚本——“三剑客”之awk命令
Applets: lifecycle
Method of designing test cases
How to describe a bug and the definition and life cycle of bug level
On cookies and session
Qgraphicsview promoted to qchartview
Technology sharing | common proxy tools for interface testing
网络——传输层(详细版)
技术分享 | 服务端接口自动化测试, Requests 库的这些功能你了解吗?