How to use ‘awk’ to print columns from a text file (in any order)

Unix/Linux FAQ: How can I print columns of data from text files on Unix/Linux systems?


One of my favorite ways to use the Unix awk command is to print columns of data from text files, including printing columns in a different order than they are in in the text file. Here are some examples of how awk works in this use case.

awk column printing examples

Suppose you have a file named foo with these contents, three columns of data separated by blanks:

$ cat foo
1 2 3
a b c

Next, here are the awk examples that show how to print the columns of data:

$ awk '{ print $1 }' foo

$ awk '{ print $2 }' foo

$ awk '{ print $3 }' foo

$ awk '{ print $1, $3 }' foo
1 3
a c

$ awk '{ print $3, $1 }' foo
3 1
c a

As you can see, with awk you can print any column you want, and you can easily rearrange the order of the columns when you print them out.

If you’re not familiar with awk, notice how it automatically loops over every line in the text file. This built-in ability to process every line in the input file is a great feature of awk.

While all of these examples show how awk works on a file, it can also read its input from a Unix pipeline, like this:

$ cat foo | awk '{ print $3, $1 }'
3 1
c a

Using a field separator with awk

If you need to specify a field separator — such as when you want to parse a pipe-delimited or CSV file — use the -F option, like this:

awk -F "|" '{ print $4 }'


As shown, awk is a great tool for printing columns and rearranging column output when working with text files.