Shell awk command

Posted by Siggles on Wed, 29 Dec 2021 07:12:54 +0100

catalogue

1, Working principle

2, Command format

3, Common built-in variables

4, Output text by line

5, Output text by field

6, Calling shell commands through pipes, double quotes

1, Working principle

  • Read the text line by line. By default, the text is separated by a space or tab key. Save the separated fields to the built-in variable, and execute the editing command according to the mode or condition.
  • The sed command is often used to process the whole line, while awk prefers to divide a line into multiple "fields" and then process them. The reading of awk information is also read line by line, and the execution results can be printed and displayed through the print function.
  • In the process of using awk command, you can use logical operators "& &" to represent "and", "11" to represent "or" and "!" Means "not"; Simple mathematical operations can also be performed, such as +, -, *, /, sign and ^ respectively represent addition, subtraction, multiplication, division, remainder and power.

2, Command format

wk option'Mode or condition{operation}'File 1 file 2...

awk-f Script file 1 file 2..

3, Common built-in variables

FS: Column delimiter. Specifies the field separator for each line of text, which defaults to spaces or tab stops. And"- F"Same effect.

NF: The number of fields in the currently processed row.

NR: The line number of the currently processed line(Ordinal number). 

$O: The entire line content of the currently processed line.

$n: The second row of the current processing line n Fields(The first n column) . 

FILENAME: The name of the file being processed.

RS: Line separator. awk From file.When reading data on,Will be based on RS The definition of cuts data into many records,and awk- Only one record is read in at a time,For processing. The default is'\n'. 

4, Output text by line

awk ' {print}' testfile2   #Output all content

awk '{print $0}' testfile2   #Output all content

Double click to hide white space

awk 'NR==1, NR==3{print}' testfile2   #Output lines 1 ~ 3

awk ' (NR>=1)&& (NR<=3) {print}' testfile2   #Output lines 1 ~ 3

awk 'NR==11 |NR==3{print}' testfile2  #Output the contents of line 1 and line 3

awk ' (NR&2)==1{print}' testfile2  #Output the contents of all odd rows

awk ' (NR&2)==0{print}' testfile2   #Output the contents of all even lines

awk ' /^root/ {print}' /etc/passwd   #Output lines starting with root

awk ' /nologin$/ {print}' /etc/passwd   #Output lines ending with nologin

awk 'BEGIN {x=0};/\/bin\/bash$/{x++};END {print x}' /etc/passwd   #Count the number of lines ending in / bin/bash, which is equivalent to grep -c"/bin/bash$" /etc/passwd


BEGIN The pattern indicates that it needs to be executed before processing the specified text BEGIN Action specified in mode: awk Reprocess the specified text before execution END The action specified in the mode, END{}Statements such as print results are often placed in statement blocks

 

 

awk 'BEGIN {x=0};/\/bin\/bash$/{x++};END {print x}' /etc/passwd   #Count the number of lines ending in / bin/bash, which is equivalent to grep -c

"/bin/bash$" /etc/passwd

BEGIN The pattern indicates that it needs to be executed before processing the specified text BEGIN Action specified in mode: awk Reprocess the specified text before execution END The action specified in the mode, END{}Statements such as print results are often placed in statement blocks.

Example

You can use the old method grep # wc-l # or - c to verify, and the results are the same.

 

 

5, Output text by field

awk -F ":" ' {print $3}' /etc/ passwd  #Outputs the third field in each row (separated by spaces or tab stops)

awk -F ":"'{print $1,$3}' /etc/passwd  #Output the 1st and 3rd fields in each row

awk -F ":" '$3<5{print $1,$3}' /etc/passwd   #Output the contents of the 1st and 3rd fields whose value of the 3rd field is less than 5

awk -F ":" '! ($3<200) {print}' /etc/passwd   #Output the row whose value of the third field is not less than 200

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

awk -F ":" '{max= ($3>=$4) ?$3:$4; {print max}}' /etc/passwd    # ($3>$4) ?$ 3:$4; Ternary operator, if the value of the third field is greater than or equal to the value of the fourth field, the third
 The value of the field is assigned to max, Otherwise, the value of the fourth field is assigned to max

awk -F ":" ' {print NR, $0}' /etc/passwd   #Output the content and line number of each line. After processing -- records, add 1 to the NR value

awk -F ":" '$7~"/bash"{print $1}' /etc/ passwd    #The output is colon delimited and the seventh field contains the first field of the row of / bash

awk -F ":" ' ($1~"root")&& (NF==7) {print $1,$2}' /etc/passwd #Output the first and second fields of the row with root and 7 fields in the first field

awk -F ":" ' ($7!="/bin/bash")&& ($7!="/sbin/nologin") {print}' /etc/passwd   #The seventh field of the output is not all rows with either / bin/bash or / sbin/ nologin

 

 

 

Example

The first method

Output the first and third fields of rows with UID number greater than or equal to 1000

Note: conditions are placed before {}

The second method uses BEGIN

View the row containing root

 

 

awk -F ":" ' ($7!="/bin/bash")&& ($7!="/sbin/nologin") {print}' /etc/passwd
#The seventh field of the output is not all rows with either / bin/bash or / sbin/nologin

Example

 

 

6, Calling shell commands through pipes, double quotes

echo $PATH | awk 'BEGIN{RS=":"};END{print NR} '    #Count the number of text paragraphs separated by colons. In the END {} statement block, statements such as print results are often placed
 
awk -F: '/bash$/{print 1 "wc -l"}' /etc/passwd     #Call the wc -1 command to count the number of users using bash, which is equivalent to grep -c "bash$"/etc/passwd

free -m | awk '/Mem:/ {print int($3/($3+$4)*100)"%"}'    #View current memory usage percentage

top -b -n 1 | grep Cpu | awk -F ',' '{print $4}' | awk '{print $1}'    #View the current CPU idle rate (-b -n 1 indicates that only one output is required)

date -d "$(awk -F "." '{print $1}' /proc/uptime) second ago" +"%F  &H: &M:%S"
#Displays the last system restart time, which is equivalent to uptime; second ago is the time before the display, and + "% F% H: & M:% s" is equivalent to the time format of + "8y-8m-&d No. H: & M: & S"

date -d "$(date -d"1 month" +"8Y*m01") -3 day" +"8Y8m number d"The penultimate day of the month

date +"number Y number m01"  First day of the month

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

awk ' BEGIN {"hostname" | getline ; {print $0}}'     #Call hostname and output the current hostname

seq 10 | awk ' {getline; print $0}'   #Get even rows
seq 10 | awk '{print $0; getline} '   #Get cardinality row

Example

View memory usage

 

 

 

Example

Output even lines: send 1-10, awk reads the first line, getline gets the second line, awk reads the third line, getline gets the fourth line, print the fourth line, and so on

Output odd rows: send 1-10, awk reads the first row, print prints the first row, getline gets the second row, awk reads the third row, print prints the third row, and so on

 

When getline No redirection left or right“<"Or“|"When, awk First read the first line, which is 1, and then getline, You get the second line below 1, which is 2, because getline After that, awk Will change the corresponding NE,NR, FNR and $0 And other internal variables, so at this time $0 The value of is no longer 1, but 2, and then print it out.

When getline There are redirection characters on the left and right“<"Or“|"When, getline It works on the directional input file. Because the file has just been opened, it has not been deleted awk Read in one-OK, just getline Read so getline Returns the first of the file-Lines, not interlaced.

FNR: awk The number of currently read records whose variable value is less than or equal to NR (For example, when reading the second file, FNR Is from 0
 Start counting again, and NR can't). 

NR==ENR: It is used to judge whether the first file is being read when reading two or more files.
awk 'BEGIN{a[0]=10;a[1]=20; print a[1]}'

awk 'BEGIN{a[0]=10;a[1]=20; print a[0}'

awk 'BEGIN{a["abc"]=10;a["xyz"]=20;print a["abc"]}'

awk 'BEGIN{a["abc"]=10;a["xyz"]=20;print a["xyz"]} '

awk ' BEGIN{a ["abc"]="aabbcc";a["xyz"]="xxyyzz";print a["xyz"]} '

awk 'BEGIN{a[0]=10;a[1]=20;a[2]=30;for(i in a) {print i,a[i]}} '

PS1: BEGIN The commands in execute only once-second

PS2: awk The subscript of the array can use either a number or a string. The string needs to use double quotation marks

 

​BEGIN Commands in mode are executed only once

awk The subscript of the array and the value of the element in the array can be either a number or a string. The string needs to be enclosed in double quotation marks

awk The array name of the array saves the list of subscripts, and the value of the corresponding element uses the array name[subscript]express

​
​
use awk Statistics httpd Access each client in the log IP Number of occurrences of?

# awk '{ip[$1]++}END{for(i in ip) {print ipli],i}' /var/log/httpd/access_log | sort -r

remarks:Define an array with the name ip,The subscript of the number is column 1 of the log file(That is, the of the client IP address),++The purpose of is to count the clients IP The counter is incremented by 1 once it appears. END The instructions in are executed after reading the file, and all statistical information is output through a loop, for Loop through the array name ip Subscript of.

​
cpu load,Memory capacity,Hard disk space,Network card traffic,Number of packages installed,Number of accounts,Number of accounts currently logged in,Number of processes,Host with wrong password

uptime,free -m,df -h, ifconfig ens33, rpm -qalwc -l,/etc/passwd, who,ps aux, /var/log/ secure


ip=i ifconfig ens33 | awk ' /inet /{print $2}'

echo "local IP the address is:"$ip

cpu='uptime | awk ' {print $NF}

#In awk, NF is the number of columns in the current row, and $NE is the last column

echo "Local machine CPU The load in the last 15 minutes is:"$cpu

net_in='ifconfig ens33 1 awk '/RX p/{print $5}''

echo "Inbound network card traffic is:"$net_ in

net_out='ifconfig ens33 | awk ' /TX p/{print $5}',

echo "Outbound network card traffic is: "$net_ out

mem='free -mI awk ' /^Mem/{print $4) '

echo"The remaining memory capacity is:"$mem

disk='df -h | awk ' /sda1/{print $4}',

echo"The remaining capacity of the root partition is:"$disk

user='cat /etc/passwd |wC - l'

echo"The number of local accounts is:"$user

login='who | wc -l'

echo "The number of accounts currently logged in to the computer is:"$login :

process='ps aux | wc -l'

echo "The number of processes started by the current computer is: "$process

soft=i rpm -qa | wc -l"

echo"The number of software installed on the current computer is:"$soft

Topics: Linux Operation & Maintenance shell server