skip to content

awk in Linux: Master the Filterering Process

/ 2 min read

awk is a programming language and text-processing tool that excels at filtering, extracting, and manipulating text. Lets learn how to use it:

Introduction to awk in Linux

While scrolling through my Discord server, I stumbled upon a question about filtering command-line output. Rather than answering directly, I decided to write this article, so that more people could benefit from the answer. So, dear reader, I hope you find this useful too!

What is awk?

awk is a programming language and text-processing tool that excels at filtering, extracting, and manipulating text. Originally created in the 1970s, it has since become a cornerstone in UNIX and Linux text processing. While awk shares some characteristics with other command-line tools like sed and grep, it is far more powerful and flexible.

Comparing with sed and grep

While sed excels at text substitution and grep is mainly used for text searching, awk can do both and much more. It can process a text file row by row and field by field, making it extremely suitable for data manipulation.

Code Examples with Outputs

Let’s look at some code examples to better understand awk. Each command will also show its expected output.

Filtering lines containing “at”

printf 'gate\napple\nwhat\nkite\n' | awk '/at/'

Output:

gate
what

This is equivalent to using grep 'at' or sed -n '/at/p'.

Filtering lines NOT containing “e”

printf 'gate\napple\nwhat\nkite\n' | awk '!/e/'

Output:

what
kite

This is similar to using grep -v 'e' or sed -n '/e/!p'.

Printing the first field of each line

echo -e "field1 field2 field3\nfieldA fieldB fieldC" | awk '{print $1}'

Output:

field1
fieldA

This will print “field1” and “fieldA”, which are the first fields in each line.

Printing the sum of the first and second fields

echo -e "10 20\n30 40" | awk '{print $1 + $2}'

Output:

30
70

This will take the sum of the first two fields in each line and print them.

Closing Remarks

awk is an incredibly powerful tool, and we’ve only scratched the surface here. With its programming facilities, you can write complex scripts that go far beyond basic text filtering and manipulation.

I hope this introduction has given you an understanding of what awk can offer!