Total visit on this blog

Wednesday 20 June 2012

AWK tutorial

Awk Introduction and Printing Operations


awk is a powful Unix command. It allows the user to manipulate files that are structured as columns of data and strings. Once you understand the basics of awk you will find that it is surprisingly useful. awk stands for the names of its authors “Aho, Weinberger, and Kernighan”
The Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then perform associated actions.
Some of the key features of Awk are:
  • Awk views a text file as records and fields.
  • Like common programming language, Awk has variables, conditionals and loops
  • Awk has arithmetic and string operators.
  • Awk can generate formatted reports
Awk reads from a file or from its standard input, and outputs to its standard output. Awk does not get along with non-text files.


The basic syntax of AWK:
awk 'BEGIN {start_action} {action} END {stop_action}' filename


Here the actions in the begin block are performed before processing the file and the actions in the end block are performed after processing the file. The rest of the actions are performed while processing the file.

Syntax:

awk '/search pattern1/ {Actions}
     /search pattern2/ {Actions}' file
In the above awk syntax:
  • search pattern is a regular expression.
  • Actions – statement(s) to be performed.
  • several patterns and actions are possible in Awk.
  • file – Input file.
  • Single quotes around program is to avoid shell not to interpret any of its special characters.

Awk Working Methodology

  1. Awk reads the input files one line at a time.
  2. For each line, it matches with given pattern in the given order, if matches performs the corresponding action.
  3. If no pattern matches, no action will be performed.
  4. In the above syntax, either search pattern or action are optional, But not both.
  5. If the search pattern is not given, then Awk performs the given actions for each line of the input.
  6. If the action is not given, print all that lines that matches with the given patterns which is the default action.
  7. Empty braces with out any action does nothing. It wont perform default printing operation.
  8. Each statement in Actions should be delimited by semicolon.

Awk Examples

Create a file example with the following data. This file can be easily created using the output of ls -l.
-rw-r--r-- 1 owner owner 12 Jun  8 21:39 p1
-rw-r--r-- 1 owner owner 17 Jun  8 21:15 t1
-rw-r--r-- 1 owner owner 26 Jun  8 21:38 t2
-rw-r--r-- 1 owner owner 25 Jun  8 21:38 t3
-rw-r--r-- 1 owner owner 43 Jun  8 21:39 t4
-rw-r--r-- 1 owner owner 48 Jun  8 21:39 t5

This include rows and columns. Each column is separated with a single space and each row is separated with new line character. We will use this file as the input for the example discussed here. 

1. awk '{print $1}' example

Here $1 has a meaning. $1, $2, $3... represents the first, second, third columns... in a row respectively. This awk command will print the first column in each row as shown below.
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--

To print the 4th and 6th columns in a file use awk '{print $4,$5}' example

Here the Begin and End blocks are not used in awk. So, the print command will be executed for each row it read from the file.


2. awk '{ if($9 == "t4") print $0;}' example


This awk command checks for the string "t4" in the 9th column and if it finds a match then it will print the entire line. The output of this awk command is


-rw-r--r-- 1 owner owner 43 Jun 8 21:39 t4




No comments:

Post a Comment