awk
eharetea

A brief overview of the awk command.

AWK is an amazing command and programming language.

Date Created:Friday December 29th, 2006 03:41 AM
Date Modified:Wednesday July 30th, 2008 04:34 PM

prints lines matching pattern

awk '/pattern/{print}' infile



prints lines matching ray_shader

awk '/ray_shader/{print}' test.ifd



prints first field of line matching ray

awk '/ray/{print $1}' test.ifd



VARIABLES:

fields are $1 through $NF

whole line is $0

NR = number of records, current line

NF = number of fields in the current record

FS = the field separator, blank by default

RS = the record separator, newline by default

ARGC = count of the number of arguments

ARGV = the array of arguments ( V is for Vector )

ARG[1] = $1

FILENAME = name of the current input file

awk '/ray/{print NR}' test.ifd

awk '/ray/{print NF $1 NR}' test.ifd



print lines without match

awk '!/houdini/{print NF $1 $2 $3 NR}' test.ifd

awk ' ! /r/{print $NF $1 NR}' test.ifd



print lines without pattern ray or shadow

awk ' ! /(ray|shadow)/{print $NF $1 NR}' test.ifd



more examples:

awk '/(opac|rough)/{print "Number Fields:" " "  NF " " $0 " " "Line Number:" NR}' noisey.vfl







*many ways to do the same trick:

sed -e 's/rough/Roughness/g' noisey.vfl

cat noisey.vfl | sed -e 's/rough/Roughness/g'



*count the lines that match normalize

awk '/normalize/{print}' noisey.vfl | wc -l



*display all without first field

awk '{$1 = "";print}' noisey.vfl



*length is an awk utitity

*this will print lines with more than 20 characters:

awk 'length > 20{print $0}' noisey.vfl



*this will print length of characters for lines that have more than 20 characters:

awk 'length > 20{print length($0)}' noisey.vfl



*print all fields of lines between these start/stop pairs:

awk '/Cf/, /Of/' noisey.vfl

awk '/pragma/, /surface/' noisey.vfl



*prints same as above but just first field

awk '/pragma/, /surface/ {print $1}' noisey.vfl



*the -F option for specifying what delimits the fields

*this prints lines where first field is Cf

awk -F" " '$1=="Cf"{print}' noisey.vfl

*this prints lines with the pattern "=", and also uses "=" as the delimiter

awk -F"=" '/=/{print}' noisey.vfl

awk -F"=" '/=/{print $1" " "="  " " $2}' noisey.vfl





USING YES, HEAD AND AWK

yes - output a string repeatedly until killed

head - output the first part of files

this prints hi 5 times



[houdini@houdini hip]$ yes | head -5 | awk '{ print "hi" }'

hi

hi

hi

hi

hi

[houdini@houdini hip]$





Uses sed to take/place the "/" and awk to return all but the last argument:

echo $1 | sed 's@/@ @g' | awk '{ for ( i = 1; i < NF; ++i) print $i}' |xargs |sed 's@ @/@g'