#156 Displaying and filtering the content of files with awk

Using the following command, we can print all lines from the file:

awk ' { print } ' /etc/passwd

This is equivalent to using the $0 variable. The $0 variables refers to the complete line.

awk ' { print $0 } ' /etc/passwd

If we want to print only the first field from the file, we can use the $1 variable. However, we need to specify that in this file the field separator used is a colon.

awk -F":" '{ print $1 }' /etc/passwd

We can do it using BEGIN block:

awk ' BEGIN {FS=":"} { print $1 } ' /etc/passwd

The code with the BEGIN and END blocks is processed just once, whereas the main block is processed for each line.

awk ' BEGIN {FS=":"} { print $1 } END {print NR} ' /etc/passwd

The awk internal variable NR maintains the number of processed lines.

awk ' BEGIN {FS=":"} { print $1 } END {print "Total number of lines:",NR} ' /etc/passwd

We can easily display the running line count with each line:

awk ' BEGIN {FS=":"} { print NR,$1 } END {print "Total number of lines:",NR} ' /etc/passwd

If we want to print only the first five lines, we use the following code

awk ' NR < 6 ' /etc/passwd

If we want to print lines 8 through to 12, we use the following code:

awk ' NR==8,NR==12 ' /etc/passwd

We can use regular expressions to match the text in the lines. Let’s look for lines that end with the word “bash”:

awk ' /bash$/ ' /etc/passwd

Discover more from Tips and Hints for Aerospace Engineers

Subscribe now to keep reading and get access to the full archive.

Continue reading