Awk
Awk program structure
the input file will be feed into the program and it will run through each line e.g with the following input file
and the following awk command:
It will output the POST line, but only the second column/field
This is very userful if you just want to output part of the string rather than grep it will output the matching line.
Fields
These are basically columns, by default its space is the seperator, but you can modify the sperator by using -F options - -F, will make it treat each comma as seperator.
Above is a typical CSV file, awk have some predefined variable to reference the above fields, consider the above with -F, - a comma as seperator.
So to print out all the name will be
See the pattern part can be omit, so it will run each line against the action. Pattern can be specified to find out the names that have second field/column as Bath
This is very useful when you can pipe it into uniq command or wc command.
Patterns
Awk support regular expression - it will depend on what version of awk you have, but they should all support the most basic ones
it also support boolean expressions and operators
all these can be combine
Will match POST or when the debug variable is true
Actions
Action statements are enclosed by curly bracket - { Action; Action; Action } There are tons awk actions, here are some common ones
Conditional:
Loops:
As you can see from the actions, it resemble a tiny programming language, but all variables in awk are global, so be careful.
Functions
They just like normal functions in any C like language
and it can be call inside the Action part
Special Variables
These are special variables, some can be modify and some can’t, see MAN page for more detail