"Shell" Awk command

1. Awk

How Awk works:

Read text line by line, separated by space or tab key as the separator by default, save the separated fields into built-in variables, and execute editing commands according to mode or condition.

The sed command is often used to process a whole line, while awk tends to divide a line into multiple "fields" and then process it. The reading of awk information is also read line by line, and the execution result can be printed and displayed by the function of print day. In the process of using the awk command, you can use logical operators "&&"表示"与"、"| |"表示"或"、"!"表示"非"and perform simple mathematical operations

Awk command format:

awk   选项    '模式或条件  [操作]'   文件1     文件2  ...
awk   -f      脚本文件       文件1    文件2     ...

Awk's common built-in variables (which can be used directly) are as follows:

character explain
FS Column separator. Specifies the field separator for each line of text, defaults to spaces or tabs.与"-F"作用相同
NF Fields of the currently processed row个数
NR currently processed 行的行号(ordinal)
$0 of the currently processed row整行内容
$n Fields of the currently processed 第n个row (column n)
FILENAME the filename being processed
RS line separator. When awk reads data from a file, it will cut the data into many records according to the definition of RS, and awk only reads one record at a time for processing. The default value is '\n'

2. Awk output text by line

1. Output everything:

awk '{print}' shicao1.txt           //输出所有内容
awk '{print $0}' shicao1.txt     //输出所有内容

insert image description here
2. Output the contents of lines 4~6

awk 'NR==4,NR==6 {print $0}' shicao1.txt

insert image description here
3. Output the content of lines 4~6 (logical AND)

awk 'NR>=4&&NR<=6 {print $0}' shicao1.txt

insert image description here
4. Output the contents of line 4 and line 6 (logic or)

awk 'NR==4||NR==6{print}' shicao1.txt

insert image description here
5. Output the contents of all odd/even lines:

awk '(NR%2)==1 {print $0}'  shicao1.txt    //输出所有奇数行的内容
awk '(NR%2)==0 {print $0}'  shicao1.txt    //输出所有偶数行的内容

insert image description here
5. Output all even lines and display the line number

awk '(NR%2)==0 {print NR,$0}'  shicao1.txt

insert image description here
6. Output lines starting with root

awk '/root/ {print $0}' /etc/passwd

insert image description here
7. Do not display the content of the tenth line

awk 'NR!=10{print $0}' shicao1.txt

insert image description here

8. The output ends with bash:

awk '/bash$/ {print}' /etc/passwd

insert image description here
9. Before processing files. The operation performed by processing the file, the END operation

awk 'BEGIN {x=0}; /nologin$/ {x++;print x,$0}; END {print x}' /etc/passwd

insert image description here

3. Awk outputs text by field

awk -F: '/^root/ {print $1}' /etc/passwd               //output the first field in each line (separated by spaces or tabs)

awk -F: '/^root/ {print $3}' /etc/passwd               //output the third field in each line (separated by spaces or tabs)

awk -F: '/^root/ {print $1,$3}' /etc/passwd         //Output the contents of the first and third fields in each line

awk -F: ‘/^root/ {print $1, 3 , 3, 3 , NF}' /etc/passwd     //Output the 1st, 3rd, and last field content in each line
insert image description here

awk -F: '$3<5 {print $0}' /etc/passwd                  //Output the content of the third field whose value is less than 5

awk -F: ‘$3<5 {print $3, $1}’ /etc/passwd
insert image description here

awk -F: '!($3>10) {print $3,$1}' /etc/passwd                 //Output the content of the third field whose value is less than 5
insert image description here

awk 'BEGIN {FS=":"};{if($3>=1000){print}}' /etc/passwd                   //First process the content of BEGIN, and then print the content in the text
insert image description here

awk -F ":" '{max=($3>=$4)?$3:$4;{print max}}' /etc/passwd //($3>$4)?$3:$4; ternary operator, if the                       first If the value of the 3 fields is greater than or equal to the value of the 4th field, then assign the value of the 3rd field to max, otherwise assign the value of the 4th field to maxinsert image description here

awk -F ":" '{print NR,$0}' /etc/passwd   //Output the content and line number of each line, and add 1 to the NR value after processing a recordinsert image description here

awk -F ":" '$7~"/bash"{print $1}' /etc/passwd                      //Output the first field of the line separated by colons and the seventh field contains /bash insert image description here
awk -F ":" '($1~"root")&&(NF==7){print $1,$2}' /etc/passwd //Output the 1st and 2nd fields of the row containing root and 7 fields in the 1st field insert image description here
awk -F ":" '($7!="/bin/bash")&&($7!="/sbin/nologin"){print}' /etc/passwd //output the seventh field is neither / bin /bash, nor for all lines of /sbin/nologininsert image description here

4. Call the shell command through the pipeline and double quotes

awk '{print NR}' shicao1.txt                     //print line number

awk ‘{print NR,$0}’ shicao1.txt
insert image description here
insert image description here

free -m | awk '/Mem:/ {print int($3/($3+$4)*100)“%”}’ //                   View the current memory usage percentage
insert image description here

awk 'BEGIN {n=0 ; while ("w" | getline) n++ ; {print n-2}}'         // call the w command and use it to count the number of online users
insert image description here

CPU usage:
insert image description here
Check the current cpu usage: top | awk '{print $0}' (dynamic)
insert image description here
execute only once: top -b -n1
insert image description here

Check cpu usage:
top -b -n1 | awk '/%Cpu/ {print $8}'
top -b -n1 | awk -F, '/%Cpu/ {print $4}' | awk '{print $1}
top -b -n1 | awk -F, '/%Cpu/ {print $4}' | awk '{print 100-$1"%"}'
insert image description here

View the usage of the root directory:

df | awk ‘{print $5}’

df | awk ‘$NF==“/”{print $5}’
insert image description here

cat /proc/uptime | awk -F, '{print $1}'  
date -d "$(cat /proc/uptime | awk -F. '{print $1}') second ago"    //开机时长  

insert image description here

cat shicao2.txt | sort | uniq -c //statistics

方法1:cat shicao2.txt | awk '{a[$1]++}; END{for(i in a){print i,a[i]}} ’
方法2:cat shicao2.txt | sort | uniq -c
insert image description here
insert image description here

echo “A B C D” | awk ‘BEGIN{OFS=“,”}; {print $0}’
echo “A B C D” | awk ‘BEGIN{OFS=“,”}; {$1=$1; print $0}’

$1=$1 is used to activate the re-assignment of $0, that is to say, the change of the field $1... and the number of fields NF will prompt awk to recalculate the value of $0, which is usually done when $0 needs to be output after changing OFS
insert image description here

awk ‘BEGIN{a[0]=1; a[1]=2; a[2]=3; print a[1]}’
awk ‘BEGIN{a[0]=1; a[1]=2; a[2]=3; print a[0]}’
awk ‘BEGIN{a[0]=1; a[1]=2; a[2]=3; print a[2]}’
awk 'BEGIN{a[“abc”]=10; a[“def”]=20; a[“xyz”]=30; print a[“abc”]}
awk ‘BEGIN{a[“abc”]=10; a[“def”]=20; a[“xyz”]=30; print a[“xyz”]}’
awk ‘BEGIN{a[“abc”]=10; a[“def”]=20; a[“xyz”]=30; print a[“def”]}’
awk ‘BEGIN{a[0]=1; a[1]=2; a[2]=3; for(i in a){print i,a[i]}}’

PS1: The command in BEGIN is only executed once
PS2: The subscript of the awk array can use numbers or strings, and strings need to use double quotes
insert image description here

5. Summary:

awk  选项 '条件 {操作}'


awk  -F '分隔符'  'NR==X {print $n}'
                  != > >= < <= || &&
                  '/字符串或正则/ {....}'
                  'Sn~"字符串" {...]'
                  !~ == != > >= < <=


awk  'BEGIN{...}; 条件{...}; END{...}'
      BEGIN{
    
    ...};处理文件前的操作
      条件{
    
    ...};按照条件处理文件行内突的操作
      END{
    
    ...}  处理完文件所有行内容后的操作

Guess you like

Origin blog.csdn.net/Wanghwei17/article/details/130682912