Skip to content

Instantly share code, notes, and snippets.

@christiangenco
Created January 15, 2015 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiangenco/ae76179592bcd35f9715 to your computer and use it in GitHub Desktop.
Save christiangenco/ae76179592bcd35f9715 to your computer and use it in GitHub Desktop.
# 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