The awk command has a large range of uses. To my mind, the primary use is to quickly slice up data contained in text files (though output from other commands can be piped through it).
| Variable | Explanation |
|---|---|
| FS | Field separator; the delimiter used to split input fields. The default in awk is whitespace. It can be set using -v FS=',' |
| OFS | Output Field Separator; the delimter used to split output fields. It can be set using -v OFS=',' |
| NF | Number of fields; the count of fields in the current record (row, line) |
| NR | Number of records; the running total of input records across all lines (rows etc.) |
| Variable | Explanation |
|---|---|
| FNR | Like NR, but resets to 1 for each new input file. Useful when processing multiple files and you need a per file line number |
| RS | Record separator; what splits records. Typically (and default) this is a newline (\n) |
| ORS | Output Record Separator; what splits the output. Default is \n |
| FILENAME | The name of the current file being processed |
| SUBSEP | Seperator used between indices in multi-dimensional arrays (e.g. arr[i,j]. Default is \034 (a non printing character). Rarely changed. |
| OFMT | The format string for printing numbers (defaults to "%.6g"). Controls how floats appear when converted to strings implicitly. |
| CONVFMT | Similar to OFMT but used when a number is converted to a string internally (not during printing). Also defaults to "%.6g". |
awk -v FS='\t' -v OFS='\t' '{print $(NF-1), $NF}' warning.txt
Let us break this command down.
| Option | Purpose |
|---|---|
-v |
Assigns a value to a build in awk variable prior to processing |
FS='\t' (Field Separator) |
Sets the input delimited to a tab character. By default, awk will separate on any contiguous whitespace. |
OFS='\t' (Output Field Separator) |
Sets the output field separator to a tab character. By default awk will use a single space to separate output columns |
'{print $(NF-1), $NF}' |
This section controls which columns it is that we wish to print out. This is example we want to print the NF column. NF in awk is a built in variable and represents the number of fields and so $NF is equal to the last column number, and NF-1 gives us the second to last column. Once we run this command, it means that the last two columns are printed and separated by a tab character. |
You may run in to errors if there are blank lines or malformed lines. You can check this using head -5 warning.txt | cat -A. Here is an example:
❯ head -5 warning.txt| cat -A
$
Timestamp$
Sequence$
Severity$
Event Code$
Here we see that the header 'row' starts with a blank line and each field is on a separate line (end of line is denoted by $ character).