======= awk ======= There are a few options under Gentoo [[https://www.gnu.org/software/gawk/gawk.html|gawk]] (default GNU awk), [[https://invisible-island.net/mawk/mawk.html|mawk]] (often faster than gawk) or [[https://www.cs.princeton.edu/~bwk/btl.mirror/|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](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]] 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](https://stackoverflow.com/questions/11967776/swap-two-columns-awk-sed-python-perl) awk -F $'\t' ' { t = $1; $1 = $2; $2 = t; print; } ' OFS=$'\t' input_file ===== Links ===== * [[https://twitter.com/b0rk/status/1000604334026055681|@bork awk]] * [[http://www.catonmat.net/blog/awk-one-liners-explained-part-one/|awk One-Liners Explained (Part One)]] * [[http://www.catonmat.net/blog/awk-one-liners-explained-part-two/|awk One-Liners Explained (Part Two)]] * [[http://www.catonmat.net/blog/awk-one-liners-explained-part-three/|awk One-Liners Explained (Part Three)]] * [[http://www.catonmat.net/blog/update-on-famous-awk-one-liners-explained/|awk One-Liners Explained (Bonus)]]