This is our third post on AWK scripting. In this post we will cover printf statement which very much use full for formatting output as the padding spaces before and after the column entries etc. Already covered topics in this series are

AWK scripting: What is an AWK and how to use it?
AWK scripting: 14 AWK print statment examples

printf is similar to AWK print statement but the advantage is that it can print with formatting the output in a desired manner. So before learning printf command I suggest you to learn about print command and then come to this printf statement.

printf syntax is similar to Bash, C type printf statement.

Syntax:

awk '{printf "format", Arguments}' filename

For example you want to print decimal values of column 3 then the example will be.

awk '{printf "%d", $3}' example.txt

Printf can do two things which AWK print command can’t

1)Defining type of Data.
2)Padding between columns.

AWK printf supported data types

The printf can be useful when specifying data type such as integer, decimal, octal etc. Below are the list of some data types which are available in AWK.

%i or d --Decimal%o --Octal
%x --hex
%c --ASCII number character
%s --String
%f --floating number

Note: Make sure that you pass exact data types when using corresponding formats as shown below. If you pass a string to a decimal formatting, it will print just zero instead of that string.

Lets start with some examples. for this post our test file contents are

Jones 21 78 84 77
Gondrol 23 56 58 45
RinRao 25 21 38 37
Edwin 25 87 97 95
Dayan 24 55 30 47

Example 1: Print first column values from db.txt file.

awk '{printf "%sn", $1}' db.txt

Output:

Jones
Gondrol
RinRao
Edwin
Dayan

Note: printf will not have default new line char, so you have to include tat when ever you execute printf command as shown above.

Example 2: Try printing a string with decimal format and see the difference.

awk '{printf "%dn", $1}' db.txt

Output:

0
0
0
0
0

So be careful when dealing with different data types.

Padding between columns using AWK printf

Let us explore the formatting the column’s available with printf statements.

Types of formatting: We can format the columns to specify number of chars each column can use. We have following padding formats available with printf statement.

-n --Pad n spaces on right hand side of a column.
n --Pad n spaces on left hand side of a column.
.m --Add zeros on left side.
-n.m --Pad n spaces right hand side and add m zeros before that number.
n.m --Pad n spaces left hand side and add m zeros before that.

Let us start exploring above mention padding’s with examples in detail.

Example4: Pad 5 spaces on right hand side of each column.

With out padding

awk '{printf "%d%d%dn", $2,$3,$4}' db.txt

Output:

217884
235658
252138
258797
245530

With padding 5 spaces on right hand side:

awk '{printf "%-5d%-5d%-5dn", $2,$3,$4}' db.txt

Output:

21   78   84
23   56   58
25   21   38
25   87   97
24   55   30

Note: As for understanding purpose we given this example with padding and without padding. We will add “|” between columnts output so that that padding can be clearly seen as show in below example.

awk '{printf "|%-5d|%-5d|%-5d|n", $2,$3,$4}' db.txt

Output:

|21   |78   |84   |
|23   |56   |58   |
|25   |21   |38   |
|25   |87   |97   |
|24   |55   |30   |

Example 5: Pad 5 spaces on left hand side of each column.

awk '{printf "|%5d|%5d|%5d|n", $2,$3,$4}' db.txt

Output:

|   21|   78|   84|
|   23|   56|   58|
|   25|   21|   38|
|   25|   87|   97|
|   24|   55|   30|

Example 6: add zero’s on left hand side of each column element make it a 5 digit number.

 awk '{printf "|%.5d|%.5d|%.5d|n", $2,$3,$4}' db.txt

Output:

|00021|00078|00084|
|00023|00056|00058|
|00025|00021|00038|
|00025|00087|00097|
|00024|00055|00030|

Example 7: Make the column element with 4 digit’s and 7 in length and print the number to left hand side.

 awk '{printf "|%-7.4d|%-7.4d|%-7.4d|n", $2,$3,$4}' db.txt

Output:

|0021   |0078   |0084   |
|0023   |0056   |0058   |
|0025   |0021   |0038   |
|0025   |0087   |0097   |
|0024   |0055   |0030   |

Example 8: Make the column element with 4 digit’s and 7 in length and print the number to right hand side.

awk '{printf "|%7.4d|%7.4d|%7.4d|n", $2,$3,$4}' db.txt

Output:

|   0021|   0078|   0084|
|   0023|   0056|   0058|
|   0025|   0021|   0038|
|   0025|   0087|   0097|
|   0024|   0055|   0030|

Hope these examples help you to understand about formatting output and padding using printf statement in AWK scripting. Keep visiting www.linuxnix.com for more scripting tutorials and tips.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.