Linux common commands awk Start Basics Tutorial

awk command tool

In Linux / UNIX systems, awk is a powerful tool for editing, the input text is read line by line, and to find the matching according to the specified pattern to meet the requirements of the output format or content filtering process can not interact the case of implementing fairly complex text manipulation, are widely used in Shell script to complete a variety of automated configuration tasks.

A, awk syntax of the command and overview

awk option 'edit mode or conditional instruction {}' file1 file2 ... 

// content filtering and output files that meet criteria

awk -f script file 1 file 2 ...

// call the editing instructions from the script, content filtering and output

* Awk execution results may be displayed by the print data printing field function. In using awk command you can use the logical operator "&&" and "||";

It may be a simple mathematical operation, such as +, -,, /,%, ^ respectively add, subtract, multiply, divide, modulo, power. **

awk reads from the standard input file or input information, and as sed, the information is read line by line read. The difference is, awk command line of the text file as a record, but a certain part of the row (column) as a field of the record. To operate these different fields (columns), a method similar to the position of the shell variables awk borrowed by $ 1, $ 2 ... $ 9 represents a procedure of a different column, a whole line of $ 0. Different fields and different fields may be separated by a specified manner, awk default delimiter is a space. awk command allows the use form "-F separator" to specify the delimiter.

Two, awk command usage examples

1) row output text

1, the output of all content

[linuxmi@linux:~/linuxmi迷]$ awk '{print}' linuxmi.py

[linuxmi@linux:~/linuxmi迷]$ awk '{print $0}' linuxmi.py

Linux common commands awk Start Basics Tutorial

Equivalent to "linuxmi.py"

2, first to third content output lines

[Linuxmi @ linux: ~ / linuxmi 迷] $ awk 'NR == 1, NR == 3 {print}' linuxmi.py

Linux common commands awk Start Basics Tutorial

Including blank lines

3, the line 3, the contents of the first row 5

[Linuxmi @ linux: ~ / linuxmi 迷] $ awk 'NR == 3 || NR == 5 {print} 'linuxmi.py

Linux common commands awk Start Basics Tutorial

4, the output from all the odd rows

[Linuxmi @ linux: ~ / linuxmi 迷] $ awk '(NR% 2) == 1 {print}' linuxmi.py

Linux common commands awk Start Basics Tutorial

5, the output of the contents of all the even lines

[Linuxmi @ linux: ~ / linuxmi 迷] $ awk '(NR% 2) == 0 {print}' linuxmi.py

Linux common commands awk Start Basics Tutorial

6, the output line to "root" at the beginning of

[linuxmi@linux:~/linuxmi迷]$ awk '/^root/{print}' /etc/passwd

Linux common commands awk Start Basics Tutorial

7, output end "nologin" line

[linuxmi@linux:~/linuxmi迷]$ awk '/nologin$/{print}' /etc/passwd

Linux common commands awk Start Basics Tutorial

8, counts the number of lines to / bin / bash at the end of

[linuxmi@linux:~/linuxmi迷]$ awk 'BEGIN {x=0} ;/\/bin\/bash$/{x++};END {print x}' /etc/passwd

9, the statistics the number of rows to / bin / bash at the end of

[linuxmi@linux:~/linuxmi迷]$ grep -c "/bin/bash$" /etc/passwd

Linux common commands awk Start Basics Tutorial

10, paragraph count the number of files separated by spaces

[linuxmi@linux:~/linuxmi迷]$ awk 'BEGIN{RS=""}; END{print NR}' /etc/dhcp/dhclient.conf

Linux common commands awk Start Basics Tutorial

Note: When more command, use the "BEGIN ...... END"

2) Press the output text field

1, the output of each line (separated by spaces) of three fields

[linuxmi@linux:~/linuxmi迷]$ awk '{print $3}' linuxmi.py

Linux common commands awk Start Basics Tutorial

2, the output of each line (separated by spaces) the first and third fields

[linuxmi@linux:~/linuxmi迷]$ awk '{print $1,$3}' linuxmi.py

Linux common commands awk Start Basics Tutorial

3) through a pipe, call the Shell command double quotes

1, calls "wc -l" command count the number of users using "bash" the

[linuxmi@linux:~/linuxmi迷]$ awk -F: '/bash$/{print | "wc -l"}' /etc/passwd

[linuxmi@linux:~/linuxmi迷]$ grep -c "bash$" /etc/passwd

Linux common commands awk Start Basics Tutorial

2, call the "w" command and force ah Statistics Online Users

[linuxmi@linux:~/linuxmi迷]$ awk 'BEGIN {while ("w" | getline) n++ ; {print n-2}}'

3, calls the "hostname" command, and the output of the current user name

[linuxmi@linux:~/linuxmi迷]$ awk 'BEGIN { "hostname" | getline ; print $0}'

Linux common commands awk Start Basics Tutorial

4) Use the awk command simple math

[linuxmi@linux:~/linuxmi迷]$ awk 'BEGIN{ a=9;b=8;print"(a + b)=",(a + b)}'
(a + b)= 17
[linuxmi@linux:~/linuxmi迷]$ awk 'BEGIN{ a=100;b=78;print"(a - b)=",(a - b)}'
(a - b)= 22
[linuxmi@linux:~/linuxmi迷]$ awk 'BEGIN{ a=80;b=5;print"(a / b)=",(a / b)}'
(a / b)= 16
[linuxmi@linux:~/linuxmi迷]$ awk 'BEGIN{ a=80;b=5;print"(a ÷ b)=",(a /  b)}'
(a ÷ b)= 16
[linuxmi@linux:~/linuxmi迷]$ awk 'BEGIN{ a=10;b=9;print"(a × b)=",(a * b)}'
(a × b)= 90

As shown below:

Linux common commands awk Start Basics Tutorial

Guess you like

Origin www.linuxidc.com/Linux/2020-02/162243.htm