awk is a bash program that scans files and process their content using patterns. It reads each line of a file or a group of files searching for the specified pattern and each time that it finds the pattern, it performs an associated action. This tool can extract specific lines or columns from files, merge files, search the content of one file in the other, etc.
When reading each line of the specified files, awk will separate it into fields (columns) using the blank space as a separator. If your file uses a different separator (i.e. a comma). Then you must specify your separator using the -F flag (see syntax below). The different fields will be denoted $1, $2, $3... etc. $0 will refer to the entire line. If the field separator (FS) is null, each line will be split into one field per character.
Syntax: awk [ -F fs ] [ -v var=value ] [ 'pattern {action}' ] [ files ] | [ other functions ]
Flag | Meaning |
---|---|
-F fs (optional) | Defines the input field separator to be the regular expression fs. Use this flag when the columns of your file use a separator other than a space. See examples below. |
-v var=variableName (optional) | When the value that is being search is stored in a variable, you should use this flag. See below for examples on how to use this flag. |
files | List of files to be searched. |
other functions (optional) | You can apply to the output of awk other functions such as head, tail, paste, grep, etc (some of them explained below and in the examples section) |