Low Orbit Flux Logo 2 F

Linux Command - awk

The linux awk command is used to match patterns and process text. Awk is actually an entire language for text processing and pattern matching.

We have an entire, really long, detailed Guide to Awk Here

If you want the long, detailed guide, check the link above. If you want a quick practical guide to the most common basics, keep reading this page.

Often times people use awk just to split lines of text into columns. We’re going to show you how to do that and a few other really basic things.

Common Uses

Display selected fields of a file like this. Note that they are listed with commas and the output will be automatically spaced out.



awk '{print $3, $5, $7}'  test1.txt

Control the formatting and add text with quotes like this:



awk '{print "Fields: "$3" "$5" "$7}'  test1.txt

Change the field separator like this:



awk -F/ '{print $3, $5, $7}'  test1.txt
awk -F: '{print $3, $5, $7}'  test2.txt
awk -F, '{print $3, $5, $7}'  test3.txt

You can also handle text that is piped from another command. This is often a more common use case.



ps -ef | awk '{print output $3, $5, $7}'

You can match a pattern like this. Instead of splitting out columns this will print any line that matches the pattern. This functionality is similar to the grep command.



ps -ef | awk '/tty/'

You can combine this functionality to split out select columns for select matching lines:



ps -ef | awk ' /tty/ {print $3, $5}'

Awk can also be used to substitute strings like this:



ps -ef | awk '{sub(/user1/,"user2");print }'

You can have a begin section and an end section that are each run once at the beginning and end. It would look like this:



awk 'BEGIN{printf "Col1\tCol2\tCol3\n"} {print $2"\t"$3"\t"$7} END{print "Done"}' test1.txt

You can use a variable to count total matches like this:



ps -ef | awk '/test/{++c} END {print "Total matched: ", c}'