Master the Linux ‘awk’ Command: A Comprehensive Guide

Peter Hou
2 min readMay 21, 2023

--

This article provides a comprehensive guide on the awk command in Linux, its usage, and common parameters. We delved into its history and why it's essential for text processing tasks. We also provided practical examples and demonstrated some of its advanced functionalities. This guide aims to help Linux newbies understand and effectively use the awk command.

Instructions

This article is designed to provide a detailed guide for Linux newbies who want to learn about the awk command. It includes historical background, usage, common and advanced parameters, and practical examples.

History

The awk command in Linux was introduced in the 1970s by Alfred Aho, Peter Weinberger, and Brian Kernighan (the command is named using their initials). It's a scripting language used for manipulating data and generating reports.

When and why to use it

awk is a powerful tool used for text processing. With awk, you can process text files line by line and field by field, making it extremely useful for data extraction and reporting.

How to use it

The basic syntax of the awk command is awk 'pattern {action}' file-name.

$ echo "Hello World" | awk '{print $1}'
Hello

The commonly used parameters

  • -F - Sets the field separator.
$ echo "Hello:World" | awk -F':' '{print $1}'
Hello
  • -v - Assigns a value to a variable.
$ echo | awk -v var="Hello World" '{print var}'
Hello World

Other supported parameters

  • -f - Specifies a file that contains awk script.
  • -m[fr] - Specifies a memory limit.
  • -O - Enables the optimization process.

Most common use cases

The awk command is widely used for text processing tasks such as extracting fields, calculating summaries, and formatting output.

$ cat file.txt | awk '{sum += $1} END {print sum}'
Total (if file.txt contains numeric values in the first field)

The tricky skills

While awk can be used for simple text processing tasks, it also supports advanced functionalities like arrays and functions.

$ echo -e "1\\\\n2\\\\n3\\\\n4\\\\n5" | awk '{array[i++]=$1} END {for (j=i-1; j>=0; j--) print array[j]}'
5
4
3
2
1

What needs to be noted

It’s important to remember that awk processes text line by line and field by field, and it treats each line as a separate record and each word as a separate field.

Conclusion

The awk command is a powerful and flexible tool for text processing in Linux. Although it may seem complex at first, with practice, you'll find it a vital part of your Linux toolkit.

--

--

Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.