Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# If you, like me, have always known about this `awk` thing
# when stealing one liners from Stack Overflow but always put
# off actually learning it, here's your 10 second lesson:
# Print lines in FILENAME that match pattern (the $0 is a
# variable that refers to the entire line, and can be omitted
# by default):
$ awk '/pattern/{print $0}' FILENAME
# ex: find all words that start with a capital A in the dictionary:
$ awk '/^A/{print}' /usr/share/dict/words
# Awk is also good at splitting lines by whitespace, like this:
$ date
Thu Jan 15 14:30:36 CST 2015
$ date | awk '{print $1}'
Thu
$ date | awk '{print $6}' # WHAT YEAR IS IT
2015
# Note that if there isn't a pattern, awk matches every line.
# Want more? Here's your 20 minute lesson:
# https://news.ycombinator.com/item?id=8893302
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.