Awk! Awk! Awk!

This week in between lots of Python and maths and things, I have been learning about awk. Awk is a programming language for doing stuff to text files that consist of columns of data. You can use awk at the command line.

awk '<condition> {<command>}' ./<filename>

will execute <command> on lines in the file called <filename> that meet <condition>. So if you want to print out all the lines that contain the word "banana", you would type

awk '/banana/ {print $0}' ./<filename>

Enclosing "banana" in / asks awk to search for it. Awk uses dollar symbols to refer to columns. $1 is the first column, $2 the second, and so on. $0 refers to the entire line.

awk '$1 == "10"{print $2}' ./<filename>

will print out the second column of all the lines on which the first column says "10"

...

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.