O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Printing with fixed width – right justified

As we have seen in the previous example, to add an empty space on the right side, we used a minus symbol (-) immediately after %. If we do not put any symbol after %, it makes the string right justified. The empty spaces are kept on the left side instead of the right.

For example, in the employee database emp.dat we print the first name of the employee again but this time using the right justified expression in printf with no symbol following the percentage % sign, as follows:

$ awk '{printf "|%10s|\n", $1 }' emp.dat

The output on execution of the preceding code is as follows:

|      Jack||      Jane||       Eva||      Amit||     Julie||       Ana||      Hari||    Victor||      John||     Billy||       Sam||     Ginny||     Emily||       Amy||      Vina|

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required