AWK - Pretty Printing

So far we have used AWK's print and printf functions to display data on standard output. But printf is much more powerful than what we have seen before. This function is borrowed from the C language and is very helpful while producing formatted output. Below is the syntax of the printf statement:
printf fmt, expr-list
In above syntax fmt is a string of format specifications and constants. expr-list is a list of arguments corresponding to format specifiers.

Escape Sequences

Like any string, format can also contain embedded escape sequences. Below is the list of escape sequences which are supported by AWK:

New Line

Following example prints Hello and World in separate lines using newline character:
[jerry]$ awk 'BEGIN { printf "Hello\nWorld\n" }'
On executing the above code, you get the following result:
Hello
World

Horizontal Tab

Following example uses horizontal tab to display different field:
[jerry]$ awk 'BEGIN { printf "Sr No\tName\tSub\tMarks\n" }'
On executing the above code, you get the following result:
Sr No    Name    Sub Marks

Vertical Tab

Following example uses vertical tab after each filed:
[jerry]$ awk 'BEGIN { printf "Sr No\vName\vSub\vMarks\n" }'
On executing the above code, you get the following result:
Sr No
    Name
        Sub
            Marks

Backspace

Following example prints a backspace after every field except last one. This erases the last number from first three fields. For instance Field 1 is displayed as Field, because the last character is erased with backspace. However the last field Field 4 is displayed as it is, as we did not have a \b after Field 4.
[jerry]$ awk 'BEGIN { printf "Field 1\bField 2\bField 3\bField 4\n" }'
On executing the above code, you get the following result:
Field Field Field Field 4

Carriage Return

In the following example, after printing every field, we do a Carriage Return and print the next value on top of the current printed value. This means, in the final output you will see only Field 4, as it was the last thing to be printed on top of all the previous fields.
[jerry]$ awk 'BEGIN { printf "Field 1\rField 2\rField 3\rField 4\n" }'
On executing the above code, you get the following result:
Field 4

Form Feed

Below example uses form feed after printing each field.
[jerry]$ awk 'BEGIN { printf "Sr No\fName\fSub\fMarks\n" }'
On executing the above code, you get the following result:
Sr No
    Name
        Sub
            Marks

Format Specifier

Like C-language AWK also has format specifiers. The AWK versions of the printf statement accepts the following conversion specification formats:

%c

It prints a single character. If the argument used for %c is numeric, it is treated as a character and printed. Otherwise, the argument is assumed to be a string, and the only first character of that string is printed.
[jerry]$ awk 'BEGIN { printf "ASCII value 65 = character %c\n", 65 }'
On executing the above code, you get the following result:
ASCII value 65 = character A

%d and %i

It prints only integer part of a decimal number.
[jerry]$ awk 'BEGIN { printf "Percentags = %d\n", 80.66 }'
On executing the above code, you get the following result:
Percentags = 80

%e and %E

It prints a floating point number of the form [-]d.dddddde[+-]dd.
[jerry]$ awk 'BEGIN { printf "Percentags = %E\n", 80.66 }'
On executing the above code, you get the following result:
Percentags = 8.066000e+01
The %E format uses E instead of e.
[jerry]$ awk 'BEGIN { printf "Percentags = %e\n", 80.66 }'
On executing the above code, you get the following result:
Percentags = 8.066000E+01

%f

It prints a floating point number of the form [-]ddd.dddddd.
[jerry]$ awk 'BEGIN { printf "Percentags = %f\n", 80.66 }'
On executing the above code, you get the following result:
Percentags = 80.660000

%g and %G

Uses %e or %f conversion, whichever is shorter, with non-significant zeros suppressed.
[jerry]$ awk 'BEGIN { printf "Percentags = %g\n", 80.66 }'
On executing the above code, you get the following result:
Percentags = 80.66
The %G format uses %E instead of %e.
[jerry]$ awk 'BEGIN { printf "Percentags = %G\n", 80.66 }'
On executing the above code, you get the following result:
Percentags = 80.66

%o

It prints an unsigned octal number.
[jerry]$ awk 'BEGIN { printf "Octal representation of decimal number 10 = %o\n", 10}'
On executing the above code, you get the following result:
Octal representation of decimal number 10 = 12

%u

It prints an unsigned decimal number.
[jerry]$ awk 'BEGIN { printf "Unsigned 10 = %u\n", 10 }'
On executing the above code, you get the following result:
Unsigned 10 = 10

%s

It prints a character string.
[jerry]$ awk 'BEGIN { printf "Name = %s\n", "Sherlock Holmes" }'
On executing the above code, you get the following result:
Name = Sherlock Holmes

%x and %X

It prints an unsigned hexadecimal number. The %X format uses uppercase letters instead of lowercase.
[jerry]$ awk 'BEGIN { printf "Hexadecimal representation of decimal number 15 = %x\n", 15}'
On executing the above code, you get the following result:
Hexadecimal representation of decimal number 15 = f
Now let use use %X and observe the result:
[jerry]$ awk 'BEGIN { printf "Hexadecimal representation of decimal number 15 = %X\n", 15}'
On executing the above code, you get the following result:
Hexadecimal representation of decimal number 15 = F

%%

It prints a single % character and no argument is converted.
[jerry]$ awk 'BEGIN { printf "Percentags = %d%%\n", 80.66 }'
On executing the above code, you get the following result:
Percentags = 80%

Optional Parameters with %

With % we can use following optional parameters:

Width

The field will be padded to the width. By default field is padded with spaces but when 0 flag is used, it is padded with zeroes.
[jerry]$ awk 'BEGIN { num1 = 10; num2 = 20; printf "Num1 = %10d\nNum2 = %10d\n", num1, num2 }'
On executing the above code, you get the following result:
Num1 =         10
Num2 =         20

Leading Zeros

A leading 0 (zero) acts as a flag, that indicates output should be padded with zeroes instead of spaces. Please note that this flag only has an effect when the field width is wider than the value to be printed. Below example describes this:
[jerry]$ awk 'BEGIN { num1 = -10; num2 = 20; printf "Num1 = %05d\nNum2 = %05d\n", num1, num2 }'
On executing the above code, you get the following result:
Num1 = -0010
Num2 = 00020

Left Justification

The expression should be left-justified within its field. When the input-string is less than the number of characters specified, and you would like it to be left justified i.e. by adding spaces to the right, use a minus symbol (-) immediately after the % and before the number. In below example output of the AWK command is pipped to the cat command to display END OF LINE($) character.
[jerry]$ awk 'BEGIN { num = 10; printf "Num = %-5d\n", num }' | cat -vte
On executing the above code, you get the following result:
Num = 10   $

Prefix Sign

It always prefixes numeric values with a sign, even if the value is positive.
[jerry]$ awk 'BEGIN { num1 = -10; num2 = 20; printf "Num1 = %+d\nNum2 = %+d\n", num1, num2 }'
On executing the above code, you get the following result:
Num1 = -10
Num2 = +20

Hash

For %o, it supplies a leading zero. For %x, and %X, it supplies a leading 0x or 0X respectively only if result is nonzero. For %e, %E, %f and %F, the result always contains a decimal point. For %g, and %G, trailing zeros are not removed from the result. Below simple example describes this:
[jerry]$ awk 'BEGIN { printf "Octal representation = %#o\nHexadecimal representaion = %#X\n", 10, 10}'
On executing the above code, you get the following result:
Octal representation = 012
Hexadecimal representation = 0XA

No comments:

Post a Comment