Monday, March 30, 2009

AWK me !!!

AWK is really great Language if you want to play on with text or we can say with variables in a script. Let's just start with a few tips on text manipulation :

Print column1, column4 and column9 of a data file or output of any columns list

$awk ‘{print $1, $4, $9}’ /path/to/file
OR
$cat filename |awk ‘{print $1 $4 $9}’
OR
$ls –al |awk ‘{print $1, $4, $9}’ -- Prints file_permissions,group and file_name

Syntax of running an awk program

Awk ‘program’ input file(s)

List all files names whose file size greater than zero.

$ls –al |awk ‘$5 > 0 {print $9}’

List all files whose file size equal to 512bytes.

$ls –al |awk ‘$5 == 0 {print $9}’


"AWK is a language for processing files of text. A file is treated as a sequence of records, and by default each line is a record. Each line is broken up into a sequence of fields, so we can think of the first word in a line as the first field, the second word as the second field, and so on. An AWK program is of a sequence of pattern-action statements. AWK reads the input a line at a time. A line is scanned for each pattern in the program, and for each pattern that matches, the associated action is executed." - Alfred V. Aho

For more info on awk please visit
awk.info -- the awk community portal

No comments:

Post a Comment