awk Command in Linux
AWK (Aho, Weinberger, and Kernighan) is a strong tool that can easily deal with several text-processing tasks. Whether you’re generating reports, transforming data files, or simply extracting information, AWK provides a simple yet powerful way to get the job done.
AWK reads and processes files line by line, splitting each line into fields, and comparing them against patterns specified by the user. When a match is found, AWK performs the pre-defined actions on particular lines. This makes it an invaluable tool for transforming data files and generating formatted reports.
This guide will explain the awk command along with possible usage on Linux.
awk Command in Linux
AWK is a potent text-based programming language that provides a fast and flexible way to manipulate data in Linux. The “awk” command is essential for generating reports, modifying data, and transforming files.
Syntax of AWK
The syntax of AWK is simple. A typical AWK command follows this structure:
awk options 'selection_criteria {action}' input-file > output-file
In the above command,
- “awk” can be used to process an input file using a set of options and selection criteria, act on the data that matches the criteria, and then redirect the output file.
- The selection_criteria define the patterns to match,
- The action within curly braces `{}` specifies what to do with the matched lines.
- The “>” symbol is used to redirect the processed output to a file.
How to Use awk Command in Linux?
“awk” can be used to print specific fields from a file, sum values in a column, or even perform complex pattern matching. Here are some practical examples of how AWK can be used in Linux with different options:
Example 1: Print All Lines in a File
By default, AWK prints every line from the specified file. It operates on a line-by-line basis, applying patterns as well as actions directed by the user. For instance, prints a text file located in the home directory:
awk '{print}' books.txt
This command outputs all lines in “books.txt” without any pattern matching.
Example 2: Pattern Specific Pattern
AWK shines when it comes to pattern matching. It can search for lines that match a given pattern. It prints lines that contain the word. To match specific patterns, such as lines containing the word “price“, the command “awk” is utilized. It only prints lines where the pattern is found:
awk '/price/ {print}' books.txt
This command outputs lines from “books.txt” where “price” appears, effectively filtering the data based on the specified pattern.
Example 3: Printing Specific Columns
To print specific columns from a file, users can use the print function. This is particularly useful for processing tabular data or logs where specific columns need to be retrieved. For instance, to print the first and fourth columns from a file, use the “awk” command:
awk '{print $1,$4}' books.txt
This command outputs the first and fourth fields of each line, separated by a space.
Note: Further, “awk” splits lines into fields using whitespace as the default delimiter.
However, users can specify a different field separator using the “-F” option. For instance, displays the first two fields of all lines in the “books.txt” file:
awk -F, '{print $1, $2}' books.txt
It treats commas as field separators and displays the first two fields of all lines in the file.
Example 4: Using Arithmetic Operations
Another common use is to perform arithmetic operations directly within the stream of a pipeline. It adds the numbers 5 and 10 and displays the result:
echo "5 10" | awk '{print $1 + $2}'
This command adds the numbers 5 and 10 and prints the result “15”.
Example 5: Using Conditional Statements
The “awk” supports conditionals like “if-else”. For instance, the awk command prints the first field if it’s greater than 100:
awk '{if ($1 > 100) print $1}' books.txt
Example 6: Using Loops
It can perform looping operations such as “for” and “while”. It prints each field of a line on a new line:
awk '{for (i=1; i<=NF; i++) print $i}' books.txt
The output prints each field of a line on a new line.
Example 7: Using Built-in Functions
The “awk” commands come with built-in functions for string and arithmetic operations. For instance, print the first two characters of the first field using the “awk” command:
awk '{print substr($1,1,2)}' file.txt
The output prints the first two characters of the first field in the terminal.
Bonus: Executing AWK Scripts
For more complex operations, users can write an AWK script and use the “-f” option to execute it. For example:
awk -f script.awk input-file
This executes the AWK program written in “script.awk” on the “input-file”.
AWK is not just limited to these examples; it offers a wide range of functionalities, including formatting output lines, performing string operations, and even controlling flow with conditionals and loops.
Conclusion
The “awk” command is a versatile utility available in Unix/Linux, which is used for manipulating data and generating reports. It’s a powerful scripting language that allows you to perform complex tasks with simple one-liners. It operates by scanning a file line by line, splitting each line into fields, and comparing these fields with a pattern.







