Ads Top

How To Use the AWK in Linux?


What is AWK?
AWK is an interpreted programming language. It is very powerful and specially designed for text processing. Its name is derived from the family names of its authors − Alfred Aho, Peter Weinberger, and Brian Kernighan.
The version of AWK that GNU/Linux distributes is written and maintained by the Free Software Foundation (FSF); it is often referred to as GNU AWK.
Uses of AWK:
  • Text processing
  • Producing formatted text reports
  • Performing arithmetic operations
  • Performing string operations, and many more

How To Use the AWK in Linux?

Awk uses some internal variables to assign certain pieces of information as it processes a file.

The internal variables that awk uses are:

FILENAME: References the current input file.

FNR: References the number of the current record relative to the current input file. For instance, if you have two input files, this would tell you the record number of each file instead of as a total.

FS: The current field separator used to denote each field in a record. By default, this is set to whitespace.

NF: The number of fields in the current record.

NR: The number of the current record.

OFS: The field separator for the outputted data. By default, this is set to whitespace.

ORS: The record separator for the outputted data. By default, this is a newline character.

RS: The record separator used to distinguish separate records in the input file. By default, this is a newline character.

We can change the values of these variables at will to match the needs of our files. Usually we do this during the initialization phase of our awk processing.


Using Variables

With awk, you can process text files. Awk assigns some variables for each data field found:

$0 for the whole line.
$1 for the first field.
$2 for the second field.
$n for the nth field.
The whitespace character like space or tab is the default separator between fields in awk.


This makes our expanded syntax look something like this:


awk 'BEGIN { action; }
/search/ { action; }
END { action; }' input_file

The BEGIN and END keywords are actually just specific sets of conditions just like the search parameters. They match before and after the document has been processed.

This means that we can change some of the internal variables in the BEGIN section.


Use case -1 :


For instance, the /etc/passwd file is delimited with colons (:) instead of whitespace. If we wanted to print out the first column of this file, we could type:

sudo awk 'BEGIN { FS=":"; }{ print $1; }' /etc/passwd




We can use the BEGIN and END blocks to print simple information about the fields we are printing:

sudo awk 'BEGIN { FS=":"; print "User\t\tUID\t\tGID\t\tHome\n--------------"; } {print $1,"\t\t",$3,"\t\t",$4,"\t\t",$6;} END { print "---------\nFile Complete" }' /etc/passwd



File Complete



Use case -2:

Reading The Script From a File

You can type your awk script in a file and specify that file using the -f option.

Our file contains this script:

Create a testfile using vi testfile

{print $1 " home at " $6}



$ awk -F: -f testfile /etc/passwd





Hope this tutorial is useful. Subscribe for more learnings.

3 comments:

  1. Do mention what is AWK & input other comment. For beginners it will be helpful like me

    ReplyDelete
    Replies
    1. Sure Prabhu. Thank you for suggestion. Let us do.

      Delete

Powered by Blogger.