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 prefix sign/symbol

To add any symbol or special character as prefix before the field value, we had to add that symbol before a percentage sign. This will make the prefix to be added to all the values of that corresponding field. In the following example, we use employee database file emp.dat and put the dollar ($) symbol before the last field, which contains the salary of users in USD, as follows:

$ vi printf_symbol.awkBEGIN    {    printf "|%-10s|%-8s|\n", "FNAME","SALARY"    printf "|==========|========|\n"    }    {    printf "|%-10s|$%-7d|\n", $1,$7    }$ awk -f printf_symbol.awk emp.dat

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

|FNAME     |SALARY  ||==========|========||Jack      |$2000   ||Jane      |$1800   ||Eva       |$2100   ||Amit      |$2350   ||Julie ...

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