In this part of our Linux Awk command series, we shall have a look at one of the most important features of Awk, which is field editing.
It is good to know that Awk automatically divides input lines provided to it into fields, and a field can be defined as a set of characters that are separated from other fields by an internal field separator.

If you are familiar with the Unix/Linux or do bash shell programming, then you should know what internal field separator (IFS) variable is. The default IFS in Awk are tab and space.
This is how the idea of field separation works in Awk: when it encounters an input line, according to the IFS defined, the first set of characters is field one, which is accessed using $1, the second set of characters is field two, which is accessed using $2, the third set of characters is field three, which is accessed using $3 and so forth till the last set of character(s).
To understand this Awk field editing better, let us take a look at the examples below:
Example 1: I have created a text file called tecmintinfo.txt.
# vi tecmintinfo.txt # cat tecmintinfo.txt

Then from the command line, I try to print the first, second and third fields from the file tecmintinfo.txt using the command below:
$ awk '//{print $1 $2 $3 }' tecmintinfo.txt TecMint.comisthe
From the output above, you can see that the characters from the first three fields are printed based on the IFS defined which is space:
- Field one which is “TecMint.com” is accessed using
$1
. - Field two which is “is” is accessed using
$2
. - Field three which is “the” is accessed using
$3
.
If you have noticed in the printed output, the field values are not separated and this is how print behaves by default.
To view the output clearly with space between the field values, you need to add (,)
operator as follows:
$ awk '//{print $1, $2, $3; }' tecmintinfo.txt TecMint.com is the
One important thing to note and always remember is that the use of ($)
in Awk is different from its use in shell scripting.
Under shell scripting ($)
is used to access the value of variables while in Awk ($)
it is used only when accessing the contents of a field but not for accessing the value of variables.
Example 2: Let us take a look at one other example using a file which contains multiple lines called my_shoping.list.
No Item_Name Unit_Price Quantity Price 1 Mouse #20,000 1 #20,000 2 Monitor #500,000 1 #500,000 3 RAM_Chips #150,000 2 #300,000 4 Ethernet_Cables #30,000 4 #120,000
Say you wanted to only print Unit_Price
of each item on the shopping list, you will need to run the command below:
$ awk '//{print $2, $3 }' my_shopping.txt Item_Name Unit_Price Mouse #20,000 Monitor #500,000 RAM_Chips #150,000 Ethernet_Cables #30,000
Awk also has a printf
command that helps you to format your output is a nice way as you can see the above output is not clear enough.
Using printf
to format output of the Item_Name and Unit_Price:
$ awk '//{printf "%-10s %s\n",$2, $3 }' my_shopping.txt Item_Name Unit_Price Mouse #20,000 Monitor #500,000 RAM_Chips #150,000 Ethernet_Cables #30,000
Summary
Field editing is very important when using Awk to filter text or strings, it helps you get particular data in columns in a list. And always remember that the use of ($)
operator in Awk is different from that in shell scripting.
I hope the article was helpful to you and for any additional information required or questions, you can post a comment in the comment section.
%s/n
what does the/n
means?@Praveen
\n
means a new line.Thanks for a great tutorial kili. Have you made a separate tutorial for printf?
@Praveen
You can check out Part 6 of the series, it shows how to use print: https://www.tecmint.com/use-next-command-with-awk-in-linux/
Hey Aaron, thanks for the info.
Do you have any tips for printing a string that could be random length with letters and spaces.
A file path for example.
What does “%-10s” mean?
Hello Lethargos,
%s means, substitute “%s” by given values in given order. In our case, first %s is substitute by first value ($2) and second %s is substitue by second value ($3). Sign ( + or – ) after “%” means, align to the left (-) or to the right (+ or nothing). There is a number after sign, its minimum length of string. If your data is shorter, there will be inserted spaces to fulfill the minimum length.
I hope it will help you, even with bad english :)
Awk with print f could not clear concept please tell and explain with more examples.
@Shashank
Point taken, we shall add a few more examples for printf as you have requested. Thanks for getting back to us.
I thought the text editor kile or Kate has the provision you make column wise selections for processing. Of course, ask is terminal command which makes it different.
Hope you meant awk here, yeah, it makes the difference on the command line.
Nice article…Thank you
Welcome, and thanks for liking it.