gawk command in Linux with Examples

gawk command in Linux is used for pattern scanning and processing language. The awk command requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. It is a utility that enables programmers to write tiny and effective programs in the form of statements that define text patterns that are to be searched for, in a text document and the action that is to be taken when a match is found within a line.

gawk command can be used to :

  • Scans a file line by line.
  • Splits each input line into fields.
  • Compares input line/fields to pattern.
  • Performs action(s) on matched lines.
  • Transform data files.
  • Produce formatted reports.
  • Format output lines.
  • Arithmetic and string operations.
  • Conditionals and loops.

Syntax:



gawk [POSIX / GNU style options] -f progfile [--] file ...
gawk [POSIX / GNU style options] [--] 'program' file ...

Some Important Options:

  • -f progfile, –file=progfile: Read the AWK program source from the file program-file, instead of from the first command line argument. Multiple -f (or –file) options may be used.
  • -F fs, –field-separator=fs: It uses FS for the input field separator (the value of the FS predefined variable).
  • -v var=val, –assign=var=val: Assign the value val to the variable var, before execution of the program begins.

Examples:

  • -F: It uses FS for the input field separator (the value of the FS predefined variable).
    gawk -F: '{print $1}' /etc/passwd

  • -f: Read the AWK program source from the file program-file, instead of from the first command line argument. Multiple -f (or –file) options may be used.
    gawk -F: -f mobile.txt /etc/passwd

Some Built In Variables:

  • NR: It keeps a current count of the number of input line.
  • NF: It keeps a count of the number of fields within the current input record.
  • FS: It contains the field separator character which is used to divide fields on the input line.
  • RS: It stores the current record separator character.
  • OFS: It stores the output field separator, which separates the fields when Awk prints them.
  • ORS: It stores the output record separator, which separates the output lines when Awk prints them.

Examples:

  • NR:
    gawk '{print NR "-" $1 }' mobile.txt

  • RS:
    gawk 'BEGIN{FS=":"; RS="-"} {print $1, $6, $7}' /etc/passwd

  • OFS:
    gawk 'BEGIN{FS=":"; OFS="-"} {print $1, $6, $7}' /etc/passwd

Sample More Commands with Examples:

  • Consider the following sample text file as the input file for all cases below.

    To create a text file:

    cat > mobile.txt

  • Default behavior of gawk: By default gawk prints every line of data from the specified file.
    gawk '{print}' mobile.txt

  • To print the lines matching with the given pattern:
    gawk '/Sunil/ {print}' mobile.txt 

    In the above example, the gawk command prints all the line which matches the ‘Sunil’.

  • To Split a line into fields: For each line, the gawk command splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 3 words, it will be stored in $1, $2 and $3 respectively. $0 represents the whole line.
    gawk '{print $2}' mobile.txt

    In the above example, $2 represents Monile no. field.

  • To display count of lines:
    gawk '{print NR, $0}' mobile.txt

  • To find the length of the longest line present in the file:
    gawk '{ if (length($0) > max) max = length($0) } END { print max }' mobile.txt

  • To count the lines in a file:
    gawk 'END { print NR }' mobile.txt

  • To print lines with more than 5 characters:
    gawk 'length($0) > 5' mobile.txt

Note:

  • To check for the manual page of gawk command, use the following command:
    man gawk
  • To check the help page of gawk command, use the following command:
    gawk --help



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.