#157 Formatting output in awk

Without formatting, the command look like this

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

The same command with formatting

awk ' BEGIN {FS=":"} { printf "%10s %4d %17s\n",$1,$3,$7 } ' /etc/passwd

With the header information added to the BEGIN block:

awk ' BEGIN {FS=":" ; print "%10s %4s %17s\n","Name","UID","Shell"} { printf "%10s %4d %17s\n",$1,$3,$7 } ' /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