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:

Sep 20, 2013

Text Processing Utility - CUT



CUT:
             CUT command is useful for extracting the portion of the text.

Manual Page:

$man cut
or
$man --help

Example:


$cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh

From the above output I am going to get single character from all the rows. $cat /etc/passwd | cut -c1
or
$cut -c1 /etc/passwd


From the above output I am going to extract user names .
$cat /etc/passwd | cut -d':' -f1
root
daemon
bin
..