awk examples

The Basics

An awk script usually takes the form

awk ‘pattern { action }’ file

where both pattern and action are optional. If there is no pattern, the action is performed on all lines. If there is action, the default action is to print all matching lines. Awk scripts are placed within single quotes when run in the command line to prevent the shell from interpreting it. Long awk scripts can be placed in a file and run as

awk -f the_script input_file

We can pass multiples to awk as well.

awk ‘pattern { action }’ file1 file2 file3


Print all lines in a file(cat file)
awk ‘{print $0}’ file
awk ‘{print}’ file
awk ‘1’ file
Continue reading


Convert a list to comma separated string and vice versa using sed and awk

This is a list of songs from Breaking Benjamin’s latest album Dark Before Dawn

cat list
Angels Fall
Breaking the Silence
Close to Heaven
Bury Me Alive
Never Again
The Great Divide
Ashes of Eden

Lets convert this to a string of values separated by comma
Continue reading