Table of Contents

awk

There are a few options under Gentoo gawk (default GNU awk), mawk (often faster than gawk) or nawk. They mostly all work the same. Tons of tutorials out there and it really depends on what you want to get done, a simple-ish overview and good guide is Steve's Awk Acadamy.

Sort on a Column

If you want to sort a file or list based on a particular column you can place the desired column at the start, pipe to sort and then remove the column

snippet.bash
awk -F, '{ print $3, $0 }' user.csv | sort | sed 's/^.* //'

Alternatively you can use sort directly…

snippet.bash
sort -t, -nk3 user.csv

Extract a Column

snippet.bash
# Print the 12th column ('{print $12}') of a comma separated file (-F,) called my_file.csv
awk -F, '{print $12}' my_file.csv
# As above but for multiple files and include the filename (FILENAME)
awk -F, '{print FILENAME,$12}' *.csv

Multiple Delimiters

snippet.bash
# Search for two delimiters (can be regular expressions, hence escaping the period)
awk -F'.|,' '{print $1 "\t" $13}' my_file.csv 

Switch Columns

Source

snippet.bash
awk -F $'\t' ' { t = $1; $1 = $2; $2 = t; print; } ' OFS=$'\t' input_file

Mean of the third column

snippet.bash
cat foo.txt | awk '{SUM+=$3} END {print SUM/NR}'

Links

HowTo's