Awk is a powerful text processing tool that allows for efficient manipulation and extraction of information from files, particularly for handling structured data such as log files, CSV files, and more.
One of its most versatile features is the ability to print specific fields and columns from a file based on predefined delimiters.
In this article, we will explore how to leverage Awk to print fields and columns, providing practical examples and explanations to demonstrate its effectiveness.
In Awk, a “field” refers to a specific segment of text within a line, delimited by a predefined separator such as a space, tab, or comma. Each segment is assigned a field number, with the first field being $1, the second $2, and so on.
Similarly, a “column” represents a vertical grouping of fields across multiple lines. By utilizing Awk’s capabilities, we can selectively print or manipulate these fields and columns to extract valuable information from our data.
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 Unix/Linux or do bash shell programming, then you should know what the internal field separator (IFS) variable is. The default IFS in Awk are tab and space.
To understand this Awk field editing better, let us take a look at the examples below:
Use Awk to Print Fields from File
To print specific fields from a file using Awk, you can use the “print” statement along with the desired field variables.
For instance, to print the first, second, and third fields of a file separated by a comma, we can use the following command:
awk '{print $1 "," $2 "," $3}' tecmintinfo.txt
In the above command, 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
.
One important thing to note and always remember is that the use of ($)
in Awk is different from its use in shell scripting.
In shell scripting, ($)
is used to access the value of variables, while in Awk, ($)
is used only when accessing the contents of a field, not for accessing the value of variables.
Use Awk to Print Columns from File
To print entire columns from a file, we can employ a similar approach by specifying the desired fields in the “print” statement. However, this time we consider multiple lines to collectively represent the column.
For example, to print the second and third columns of a file, we can use the following command:
awk '//{print $2, $3 }' my_shopping.txt
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 the output of the Item_Name and Unit_Price:
awk '//{printf "%-10s %s\n",$2, $3 }' my_shopping.txt
Use Awk to Print Field and Column Ranges from File
Awk also allows us to define ranges of fields or columns using the ":"
operator. For instance, to print fields 2 to 4 from a file, we can use the following command.
awk '{print $2 ":" $4}' filename
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.
For those seeking a comprehensive resource, we’ve compiled all the Awk series articles into a book, that includes 13 chapters and spans 41 pages, covering both basic and advanced Awk usage with practical examples.
Product Name | Price | Buy |
---|---|---|
eBook: Introducing the Awk Getting Started Guide for Beginners | $8.99 | [Buy Now] |
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.
The output of field ranges (from to) does not work in this way. You can see this in the examples.
%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.