What is AWK?
AWK is a data driven interpreted scripting language that was developed at Bell Labs in the 1970's.
- Created/used by Alfred Aho, Peter Weinberger and Brian Kernighan.
- Use in command line sequences or standalone to process data files.
Why Use AWK?
- Good for basic data manipulation tasks
- Always there
- Great for processing rows and columns
- Little overhead.
The Basic Form
- The basic form of an awk command can be expressed as:
- pattern { action }
- or
- condition { action }
- The pattern/condition is optional.
- Generally, the pattern/condition and braces with their contents are in single quotes if executing from the command line.
- The pattern/condition test is run on each line of input data and if the result is true, the action is executed.
Running AWK
- awk pattern/condition '{ action }' data_file_name(s)
- awk -fawk_script_namedata_file_name(s)
- The -f is followed by the name of afile containing awk commands.
- command | awk pattern/condition '{ action }'
- “action” above may be multiple actions
- Each line of input, whether from a command or from a data file, is tested based on the condition/pattern and acted on if matched or true.
AWK Example
- awk '{ print “Go Blue” }' file1
- Since there is no test condition, this will print “Go Blue” (without the quotes) for each line of file1. Nothing from file1 is printed.
- cat file1 | awk '{ print “Go Blue” }'