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

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

Alternatively you can use sort directly…

sort -t, -nk3 user.csv

Extract a Column

# 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

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

Switch Columns

Source

awk -F $'\t' ' { t = $1; $1 = $2; $2 = t; print; } ' OFS=$'\t' input_file
bash/awk.txt · Last modified: 2021/03/20 19:21 by 127.0.0.1
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0