Sep 30, 2013

Unix Utility - AWK


AWK - Aho Weinberger Kernighan

AWK is an interpreted programming language designed for text processing and typically used as a data extraction and reporting tool. 

It is a standard feature of most Unix-like operating systems. AWK was very popular in the late 1970s and 1980s, but from the 1990s has largely been replaced by Perl, on which AWK had a strong influence.

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.

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. 
Example:
 
$ cat employees.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Sanjay  Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500
500  Randy   DBA        Technology  $6,000
 
$ awk '/n/ {print $2,$NF;}' employees.txt
Thomas $5,000
Jason $5,500
Sanjay $7,000
Nisha $9,500
Randy $6,000


Good to visit:

No comments:

Post a Comment