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.
Please refer to our previous tutorials in the Awk series:
- How to Filter Text or String Using Awk and Regular Expressions – Part 1
- How to Use Awk to Print Fields and Columns in File – Part 2
- How to Use Awk to Filter Text Using Pattern Specific Actions – Part 3
- How to Use Comparison Operators with Awk in Linux – Part 4
- How to Use Compound Expressions with Awk in Linux – Part 5
- How to Use ‘next’ Command with Awk in Linux – Part 6
- How to Read Awk Input from STDIN in Linux – Part 7
- How to Use Awk Variables, Numeric, and Assignment Operators – Part 8
- How to Use Awk Special Patterns ‘BEGIN and END’ – Part 9
- How to Use Awk Built-in Variables in Linux – Part 10
- How to Allow Awk to Use Shell Variables in Linux – Part 11
- How to Use Flow Control Statements in Awk – Part 12
- How to Write Scripts Using Awk Programming Language – Part 13
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.
If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.

Got Something to Say? Join the Discussion... Cancel reply