AWK
Last updated
Last updated
It is more of a script than an command, which is heavily used for text processing and data manipulation and produce formatted reports. It is typically used as a data extraction and reporting tool. It is a standard feature of most Linux operating systems and is useful when handling text files that are formatted in a predictable way. awk parses and manipulates tabular data on a line-by-line basis, and it iterates through the entire file. By default, awk uses whitespace—for example, spaces and tabs—as a delimiter to separate fields
The syntax is as follows:
As shown above, -F is important to tell the delimiter
awk has built-in variables such as:
$0
. Used to specify the whole line.
$1
. Specifies the first field or first column.
$2
. Specifies the second field.
NR: Counts the number of input records (usually lines). Awk command performs the pattern/action statements once for each record in a file.
FS: Just like the command line argument -F, the field separator can also be passed via variable FS.
RS: Stores the current record separator character. Since, by default, an input line is the input record, the default record separator character is a newline.
OFS: Stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. Whenever print has several parameters separated with commas, it will print the value of OFS in between each parameter.
ORS: Stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character. print automatically outputs the contents of ORS at the end of whatever it is given to print.
FNR: It is the current record number in the current file. For the file, NR is going to be equal to FNR as FNR will reset to 1 for every file but NR will keep increasing.
NF: Variable whose value is the number of fields in the current record.