Monday 26 December 2016

Introduction to text formatting with printf

Printf is often touted as the successor to the unix echo command. This article will provide a very basic overview of how we can use print to better format the output of our shell commands.

Here's the basic syntax for printf:

printf "format string", variable1, variable2 

The format string is the key to the formatted output beacuse it specifies exactly how the formatted output should appear.
A format specifier is a special code that indicates what type of variable is displayed and how to display it

The format specifiers use the following format:

%[modifier]control-letter

The control-letter is a one-character code that indicates what type of data will be displayed and modifier defines an optional formatting feature.

Format Specifier Control Letters:

Control Letter Description
c Displays a number as an ASCII character
d Displays an integer value
i Displays an integer value (same as d)
e Displays a number in scientifi c notation
f Displays a fl oating-point value
g Displays either scientifi c notation or fl oating point, whichever is shorter
o Displays an octal value
s Displays a text string
x Displays a hexadecimal value
X Displays a hexadecimal value, but using capital letters for A through F

In addition to the control letters, you can use three modifi ers for even more control over the output:

width: This is a numeric value that specifies the minimum width of the output field. If the output is shorter, printf pads the space with spaces, using right justification for the text. If the output is longer than the specified width, it overrides
the width value.

prec: This is a numeric value that specifies the number of digits to the right of the decimal place in fl oating-point numbers, or the maximum number of characters displayed in a text string.

- (minus sign): The minus sign indicates that left justification should be used instead of right justification when placing data in the formatted space.


Here is an example of formatting a output of a text file using printf.

Consider the below file named testfile.

[root@cent6 ~]# cat testfile
this    is         a test               file
the     second          line
continueing     to              third           line

We can notice that the spacing between the strings in the file is highly irregular.
If we break the file into rows & columns we basically have three rows & four columns.

Now, let's use printf to format the output:

[root@cent6 ~]# cat testfile | awk '{printf "%-15s %-15s %-15s %-15s \n", $1, $2, $3, $4 }'
this            is              a               test
the             second          line
continueing     to              third           line


Let's take another example using some numrical data & this time we'll use printf directly without awk.
Consider the following file abc

[root@cent6 ~]# cat abc
10.345                  test
11.5894         another_test
12.4980             still_testing
13.58494044       testing

It consists of float type & string type data. If we wanted to print only upto two decimal places & ensure coulmn width to ten spaces, we may use the following printf command:

[root@cent6 ~]#  printf "%-10.2f %-10s\n" `cat abc`
10.35      test
11.59      another_test
12.50      still_testing
13.58      testing

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...