shell script awk command

working principle

Read text line by line, separated by space or tab by default, save the separated fields to built-in variables, and execute editing commands according to the 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 with the field data through the print function. In the process of using the awk command, you can use the logical operators "&&" to mean "and", "||" to mean "or", and "!" to mean "not"; you can also perform simple mathematical operations, such as +,- , *, /, %, ^ represent addition, subtraction, multiplication, division, remainder and power respectively.

Command format

awk option'mode or condition {operation}' file 1 file 2…
awk -f script file file 1 file 2…

Common built-in variables in awk (can be used directly)

FS: Column separator. Specify the field separator for each line of text, the default is a space or a tab stop. It has the same effect as "-F".
NF: The number of fields in the row currently being processed.
NR: The row number (ordinal number) of the row currently being processed.
$0: The entire line content of the currently processed line.
$n: The nth field (nth column) of the currently processed row.
FILENAME: The name of the file 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, while awk only reads one record at a time for processing. The default value is'\n'

Example (output text by line)

Output all content
Insert picture description here
Output content of lines 1~3
Insert picture description here
Output content of line 1 and line 3
Insert picture description here
Output content of all odd and even lines
Insert picture description here
Output lines beginning with root
Insert picture description here
Output lines ending with nologin
Insert picture description here
Count the number of lines ending with /bin/bash

Insert picture description here

等同于 grep -c "/bin/bash$" /etc/passwd
BEGIN模式表示,在处理指定的文本之前,需要先执行BEGIN模式中指定的动作;awk再处理指定的文本,之后再执行END模式中指定的动作,END{}语句块中,往往会放入打印结果等语句

Example (output text by field)

Output the 3rd field in each line (separated by spaces or tab stops)
Insert picture description here
Output the 1st and 3rd fields in each line
Insert picture description here
Output the contents of the 1st and 3rd fields where the value of the 3rd field is less than 5

Insert picture description here
Output the line where the value of the third field is not less than 200
Insert picture description here
Method 2: Process the content of BEGIN first, and then print the content in the text
Insert picture description here
($3>$4)? $3:$4 ternary operator, if the value of the third field is greater than For the value of the 4th field, assign the value of the 3rd field to max, otherwise assign the value of the 4th field to max

Insert picture description here
Output the content and line number of each line, each time a record is processed, the NR value is increased by 1

Insert picture description here
The output is separated by a colon and the first field of the line containing /bash in the seventh field is
Insert picture description here
output. The first and second fields of the line that contains root and 7 fields in the first field are
Insert picture description here
output. The seventh field is neither /bin/bash, not all lines in /sbin/nologin
Insert picture description here

Invoke Shell commands through pipes and double quotes

Count the number of text paragraphs separated by colons. In the END{} statement block, statements such as print results are often put in. The
Insert picture description here
wc -l command is used to count the number of users using bash, which is equivalent to grep -c "bash$" /etc/passwd
Insert picture description here
View the current memory usage percentage to Insert picture description here
view the current CPU idle rate, (-b -n 1 means that only 1 output is needed)
Insert picture description here
shows the last system restart time, which is equivalent to uptime; second ago is the time in seconds before the display, +"% F %H:%M:%S" is equivalent to +"%Y-%m-%d %H:%M:%S". The time format
Insert picture description here
calls the w command, and is used to count the number of online users.
Insert picture description here
Call hostname and output the current Hostname
Insert picture description here

当getline左右无重定向符“<”或“|”时,getline作用于当前文件,读入当前文件的第一行给其后跟的变量var或$0;应该注意到,由于awk在处理getline之前已经读入了一行,所以getline得到的返回结果是隔行的。
当getline左右有重定向符“<”或“|”时,getline则作用于定向输入文件,由于该文件是刚打开,并没有被awk读入一行,只是getline读入,那么getline返回的是该文件的第一行,而不是隔行。

Guess you like

Origin blog.csdn.net/MQ107/article/details/114845128