Learn AWK command — Part 1

Introduction 

We discussed about sed command to edit the files in many ways .Then what is awk? Where we will use this command? What is the difference between awk? These all questions coming to your mind ,right ? ok then we will learn about basics of awk here. Many Unix utilities generates reports in the form of rows and columns, so awk is excellent tool to process these fields while SED will process the lines.

What is AWK?

  • The awk command is derived from the names of its inventors namely Aho Weinberger and Kernighan (AWK).
  • Definition of awk is by bell labs

“Awk is a programming language designed to make many common information retrieval and text manipulation tasks easy to state and to perform.”

  • Simply put, awk is a programming language designed to search for, match patterns, and perform actions on files.
  • Like sed, awk also pattern action language.
  • The basic function of awk is to search files for lines (or other units of text) that contain certain patterns. When a line matches one of the patterns, awk performs specified actions on that line. Awk keeps processing input lines in this way until the end of the input file is reached.
  • AWK gets input from files ,redirection and pipes and Directly from standard input also

AWK features over SED:

  • The syntax of awk is similar to C language.
  • Awk has built in arithmetic and string functions.
  • Awk views a text file as records and fields
  • Convenient way of accessing fields with in lines.
  • Variables and control flow in action

AWK Versions:

  • awk – Original Bell Labs awk (Version 7 UNIX, around 1978) + latest POSIX awk.
  • nawk – New awk (released with SVR4 around 1989)
  • gawk – GNU implementation of awk standard.
  • mawk – Michael’s awk.

All these are basically same except for some minor differences in features provided


 

Structure of AWK:

Let’s see the syntax of awk command

Syntax of awk :      awk  ‘/search pattern1/ {Actions}

 

                                         /search pattern2/ {Actions}’   file

or

BEGIN {action}

Pattern {action}

Pattern {action}

.

.

.

Pattern {action}

END {action}

 

Where

BEGIN segment is optional for processing to execute prior to reading input

END segment is also optional for processing after end of input data

Search pattern is the regular expression, arithmetic relational expressions,  string-valued  expressions,  and arbitrary Boolean combinations of these

Action – Statements to be performed

File – input to the awk command.

Note:  Single quotes around program are to avoid shell not to interpret any of its special characters.

  • Awk reads from a file or from its standard input, and outputs to its standard output.
  • Awk recognizes the concepts of “file”, “record” and “field”.
  • A file consists of records, which by default are the lines of the file. One line becomes one record.
  • Awk operates on one record at a time.
  • A record consists of fields, which by default are separated by any number of spaces or tabs.
  • Field number 1 is accessed with $1, field 2 with $2, and so forth. $0 refers to the whole record.

How AWK works:

  • Awk reads the input files one line at a time.
  • For each line, it matches with given pattern in the given order, if matches perform the corresponding action.
  • If no pattern matches, no action will be performed.
  • In the above syntax, either search pattern or action is optional, But not both.
  • If the search pattern is not given, then Awk performs the given actions for each line of the input.
  • If the action is not given, print all that lines that matches with the given patterns which is the default action.
  • Empty braces without any action do nothing. It won’t perform default printing operation.
  • Each statement in Actions should be delimited by semicolon.

We will discuss more about AWK in next post . Hope i am making you understand .

Don’t forget to give your valuable feedback

 

Advertisements

Express your Opinion here

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s