# awk There are a few options under Gentoo [gawk](https://www.gnu.org/software/gawk/gawk.html) (default GNU awk), [mawk](https://invisible-island.net/mawk/mawk.html) (often faster than gawk) or [nawk](https://www.cs.princeton.edu/~bwk/btl.mirror/). 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](http://www.troubleshooters.com/codecorn/awk/index.htm). ## Sort on a Column If you want to sort a file or list based on a particular column you can [[https://stackoverflow.com/questions/17048188/how-to-use-awk-sort-by-column-3|place the desired column at the start, pipe to sort and then remove the column]] ```bash awk -F, '{ print $3, $0 }' user.csv | sort | sed 's/^.* //' ``` Alternatively you can use sort directly... ```bash sort -t, -nk3 user.csv ``` ## Extract a Column ```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 ```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](https://stackoverflow.com/questions/11967776/swap-two-columns-awk-sed-python-perl) ```bash awk -F $'\t' ' { t = $1; $1 = $2; $2 = t; print; } ' OFS=$'\t' input_file ``` ## Mean of the third column ```bash cat foo.txt | awk '{SUM+=$3} END {print SUM/NR}' ``` # Links * [@bork awk](https://twitter.com/b0rk/status/1000604334026055681) * [awk One-Liners Explained (Part One)](http://www.catonmat.net/blog/awk-one-liners-explained-part-one/) * [awk One-Liners Explained (Part Two)](http://www.catonmat.net/blog/awk-one-liners-explained-part-two/) * [awk One-Liners Explained (Part Three)](http://www.catonmat.net/blog/awk-one-liners-explained-part-three/) * [awk One-Liners Explained (Bonus)](http://www.catonmat.net/blog/update-on-famous-awk-one-liners-explained/) * [The GNU Awk User’s Guide](https://www.gnu.org/software/gawk/manual/gawk.html) ## HowTo's * [Running Awk in parallel to process 256M records](https://ketancmaheshwari.github.io/posts/2020/05/24/SMC18-Data-Challenge-4.html)