Why AWK ?
Syntax of AWK:
awk '/search pattern1/ { Actions}
awk '/search pattern1/ { Actions}' file
In The above syntax search pattern is a regular expression.
Actions - Statements to be performed. Several patterns and actions are possible in AWK.
File - Input File.
Example 1: awk '{print;}' employee.txt
Here pattern is not there so actions are applicable to all the lines.
Working Methodology of AWK :
awk '/Thomas/Nisha/' employee.txt
It will print the lines that contain Thomas and lines contains Nisha.
Example 3: Print only selective fields.
AWK treats a line as a record and columns as a field.
awk '{print $1,$5;}' employee.txt
It will print only the 2nd and 5th filed of employee.txt
Example 4: Initialization and final action
Syntax: BEGIN {Actions}
{ACTION}
END {Actions}
The Built in Variables of AWK:
1. FS (Input Filed Separator): By default AWK assumes that fields in a file are separated by space character. If the fields are separated by any other character, we can use the FS variable to tell about the delimiter.
$ awk 'BEGIN {FS="|"} {print $2}' input_file
It will print the second word of the input_file provided the field separator is "|".
- The name AWK comes from the initials of its designers. Alfred V. Aho, Peter J. Weinberger. and Brian W. Kernighan. The Original version of AWK was written in 1977.
- UNIX has many utilities and AWK is one of them.
- AWK is an excellent tool for processing rows and columns of data, and is easier to use AWK than most conventional programing languages.
- AWK can be considered to be a pseudo-C interpreter, as it understands the same arithmetic operators as C.
- AWK also has string manipulation functions, so it can search for particular strings and modify the output.
Syntax of AWK:
awk '/search pattern1/ { Actions}
awk '/search pattern1/ { Actions}' file
In The above syntax search pattern is a regular expression.
Actions - Statements to be performed. Several patterns and actions are possible in AWK.
File - Input File.
Example 1: awk '{print;}' employee.txt
Here pattern is not there so actions are applicable to all the lines.
Working Methodology of AWK :
- AWK reads the input files one line at a time.
- For each line, it matches with given pattern in the given order, if matches performs the corresponding action.
- If no pattern matches, no action will be performed.
- In the above syntax either search patterns or actions are 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 the lines that matches with the given patterns which is the default action.
- Each statement in actions should be delimited by semicolon.
- Each word in a line is a field $1, $2,..... $NF.
awk '/Thomas/Nisha/' employee.txt
It will print the lines that contain Thomas and lines contains Nisha.
Example 3: Print only selective fields.
AWK treats a line as a record and columns as a field.
awk '{print $1,$5;}' employee.txt
It will print only the 2nd and 5th filed of employee.txt
Example 4: Initialization and final action
Syntax: BEGIN {Actions}
{ACTION}
END {Actions}
The Built in Variables of AWK:
1. FS (Input Filed Separator): By default AWK assumes that fields in a file are separated by space character. If the fields are separated by any other character, we can use the FS variable to tell about the delimiter.
$ awk 'BEGIN {FS="|"} {print $2}' input_file
It will print the second word of the input_file provided the field separator is "|".