The basic usage of the awk command
The basic usage of awk
Format
Awk Action File name / File name / awk/ Action
Print current, print print, $0 current line
[root@zhaocheng ~] # echo "awk is very useful" | awk'{print $0} 'awk is very useful [root@zhaocheng ~] # cat filetest root:$1 $dDTFylQ3 $.vTZKpm7mrra9WMsxvBfW.vTZKpm7mrra9WMsxvBfW.18241TZKpm7mrra9WMsxvBfW.18241VTZKpm7mrra9WMsxvBfW.18241VTZKpm7mrra9WMsxvBfW.VTZKpm7mrra9WMsxvBfW.18241VTZKpm7mrra9WMSxvBfW.18289 : daafrabbitmq:!!:18297:dada
For example, take the first field of this file
-F delimiter, ":" with colon as delimiter
'{print $1}' prints each column, here is the first field in which each column is printed with a colon as a separator
[root@zhaocheng ~] # cat filetest | awk-F ":"'{print $1} 'rootbinlpsyncshutdownhaltnginxrabbitmq
For example, if you print the last field with this file, you still have to print with something as a separator, and some of it may be spaces.
[root@zhaocheng ~] # cat filetest | awk-F':'{print $NF} '7dad7adaggdafsdadaafdada
Or take a field in the middle, you can use $(NF-1), dealing with-1 line from back to front-1
[root@zhaocheng ~] # cat filetest | tail-1systemctl start mysqld [root@zhaocheng ~] # cat filetest | tail-1 | awk-F "'{print $(NF-1)} 'start
Or if you want to know the line of processing, you can use NR, which is equivalent to adding a ")".
[root@zhaocheng ~] # cat filetest | tail-3 | awk-F "'{print NR") "$2}'1) 2) 3) start
For example, take the IP of ifconfig. There are many ways to get IP.
[root@zhaocheng ~] # ip a | grep eth02: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 172.17.0.8 scope global eth0 20 brd 172.17.15.255 scope global eth0 here is the first field to print each column, using tail-1 as the last line, using awk-F to take the first field [root@zhaocheng ~] # ip a | grep eth0 | awk'{print $2}'| tail-1 | awk-F "/" {print $1} '172.17.0.8
Or directly use grep to filter out, use awk with a space as a separator, directly take the second field, and then take the first field of the / sign
[root@zhaocheng ~] # ip a | grep eth0 | grep inet | awk-F "'{print $2}'| awk-F" / "'{print $1} '172.17.0.8
You can also use sed to fetch its number of rows, and then use awk to match exactly
[root@zhaocheng ~] # ip a | sed-n '9p' | awk-F ""' {print $2}'| awk-F "/"'{print $1} '172.17.0.8
Other variables of awk
FS: field delimiter, default is space and tab.
RS: line separator, which is used to split each line. The default is the newline character.
OFS: the delimiter of the output field, which is used to separate fields when printing. The default is a space.
ORS: the delimiter of the output record, which is used to print the time delimited record. The default is the newline character.
OFMT: the format of the digital output, default is% .6g.
For example, print out all the lines in this file with nginx subsections.
[root@zhaocheng ~] # awk-F ":"'/ nginx/ {print $0} 'filetest nginxVRV Sdada
For example, print the line before the third line of the filetest file, and print the third field with: as the delimiter
[root@zhaocheng ~] # awk-F ":"'NR > 3 {print $3} 'filetest 1783417834178341828918297sdada