🌐 🇵🇱 Polski · 🇬🇧 EN
Most monitoring tools present data in text format. Often, we do not need all the values provided by a given tool and instead need to extract only specific ones. So, how can we narrow down the output displayed by a monitoring tool? AWK is perfect for this task. It is an interpreted programming language whose main purpose is pattern scanning and processing in files and data streams. AWK is located in the "gawk" package. AWK is excellent for working with multi-column data, such as the output from "sar" or even a simple file listing using the "ll" command. One of the most frequently used commands with awk is "print". The "print" command is used to display selected fields from the entire output generated by the preceding command. The attribute $1 denotes the first field, $2 the second, $3 the third, and so on. The last field is represented by the argument $NF. The representation of the second-to-last field is $(NF-1), and the third-to-last is $(NF-2). The entire line is denoted by $0. The default field separators in awk are whitespace characters (spaces or tabs). Changing the separator character is done using the "-F" switch. You can learn about the awk language at this address. There is also a community surrounding awk, with a website at awk.info. It is also worth checking out this interesting tutorial: http://www.awk.of.pl/.
For example, in the /etc/passwd file, the field separator is the colon ":". If we want to use awk to display only the first column containing the usernames, the command syntax will look like this:
# awk -F: '{print $1}' /etc/passwd
root
bin
daemon
...
etc...
And how do you display the first and last columns of the /etc/passwd file?
# awk -F: '{print $1, $NF}' /etc/passwd
root /bin/bash
bin /sbin/nologin
daemon /sbin/nologin
For better readability, we can also add a header and footer to the displayed results using "printf".
# awk -F: 'BEGIN {printf "Usernames and system shells for users \n"}
{print $1, $NF }
END {printf "END...\n"}' /etc/passwd
Usernames and system shells for users
root /bin/bash
bin /sbin/nologin
daemon /sbin/nologin
...
END...
Using awk, it is also possible to search through text using regular expressions. The best way to see how awk works is to run the examples.
Listing only those lines in the /etc/passwd file that start with the letter "m".
# awk -F: '/^m/ {print $0}' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
Counting the number of lines in a file using awk, similar to wc -l.
# awk 'END{print NR}' /etc/passwd
35
Calculating the sum of file sizes from the ls -l command.
# ls -l
total 52
-rw------- 1 root root 1368 Jun 12 2012 anaconda-ks.cfg
-rw-r--r-- 1 root root 29111 Jun 12 2012 install.log
-rw-r--r-- 1 root root 3861 Jun 12 2012 install.log.syslog
# ls -l | awk 'BEGIN{total=0} {total += $5} END{print total}'
34340
Displaying files owned by a specific user.
# ls -l | awk '$3 == "mariusz"'
The capabilities of this tool offer a wide range of possibilities for creating commands, and when combined with regular expressions, they allow for arbitrary processing of output data, files, or streams.
Syntax and more information can, of course, be found in the manual.

Comments