All Notes
Linux Fundamentals
Linux Fundamentals
  • Aim
  • History
  • Components of Linux OS
  • Linux Kernel
  • Linux Commands
  • AWK
  • Regex
  • Log Scanning
  • SE-Linux
Powered by GitBook
On this page

AWK

PreviousLinux CommandsNextRegex

Last updated 10 months ago

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:

awk GNU_OR_POSIX_OPTIONS 'pattern_selection_criteria {action}' input-file

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.