Printing with fixed column width 

To create a fixed-column-width report, we have to specify a number immediately after the % in the format specifier. This number shows the minimum number of characters to be printed. This is the width (minimum size) of the field. If the input in the field becomes large, it automatically grows to prevent information loss. If the input string is smaller than the specified number, spaces are added to the left.

The following example displays the basic use of printf with fixed column width using the number specified immediately after the %. We have added headers inside the BEGIN statement to make the output more readable, as follows:

$ vi printf_width.awk

BEGIN {
printf "%6s %6s %10s %17s %3s %3s %6s ",
"FName","LName","ContactNo.","EmailId","Sex","Dpt","Salary"
printf "------------------------------------------------------------------------------ "
}
{
printf "%6s %6s %10d %17s %3s %3s %4d ", $1,$2,$3,$4,$5,$6,$7
}

$ awk -f printf_width.awk emp.dat

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

 FName     LName    ContactNo.              EmailId    Sex    Dpt    Salary
------------------------------------------------------------------------------
Jack Singh 9857532312 [email protected] M hr 2000
Jane Kaur 9837432312 [email protected] F hr 1800
Eva Chabra 8827232115 [email protected] F lgs 2100
Amit Sharma 9911887766 [email protected] M lgs 2350
Julie Kapur 8826234556 [email protected] F Ops 2500
Ana Khanna 9856422312 [email protected] F Ops 2700
Hari Singh 8827255666 [email protected] M Ops 2350
Victor Sharma 8826567898 [email protected] M Ops 2500
John Kapur 9911556789 [email protected] M hr 2200
Billy Chabra 9911664321 [email protected] M lgs 1900
Sam khanna 8856345512 [email protected] F lgs 2300
Ginny Singh 9857123466 [email protected] F hr 2250
Emily Kaur 8826175812 [email protected] F Ops 2100
Amy Sharma 9857536898 [email protected] F Ops 2500
Vina Singh 8811776612 [email protected] F lgs 2300

If the input string has more characters than what we've specified as the exact width, the whole string will be printed; the output will be zigzag and not what we applied. So, we have to put exactly as many characters as we want to print.

Space is added to the left. Let us say we print hello as an eight-character string. Then three spaces will be added on the left, as follows:

$ awk 'BEGIN{printf "%8s", "hello
"}'

The output on execution of the preceding code is:

hello

The whole string is printed even if we specify a smaller character width. For example, now we extend the hello string used in the previous example to hello world, as follows:

$ awk 'BEGIN{printf "%8s", "hello world
"}'

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

hello world

To align the header and other multiple printf statements, sometimes we need to go through several rounds of trial and error.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.17.40