Awk Formatted Printing

Header

awk -F: 'BEGIN {
    printf "%-10s %s\n", "User", "Home"
    printf "%-10s %s\n", "----","----"}
    { printf "%-10s %s\n", $1, $(NF-1) }
' /etc/passwd | head -n 5

Outputs

User       Home
----       ----
root       /root
bin        /bin
daemon     /sbin

Space

awk -F: '{
 printf "%-10s %s\n", $1, $(NF-1)
}' /etc/passwd | head -n 3

Outputs

root       /root
bin        /bin
daemon     /sbin

Common specifiers

Character Description
c ASCII character
d Decimal integer
e, E, f Floating-point format
o Unsigned octal value
s String
% Literal %

Usage

#Right align

awk 'BEGIN{printf "|%10s|\n", "hello"}'
| hello|

#Left align

awk 'BEGIN{printf "|%-10s|\n", "hello"}'
|hello |
Comments