AWK is very powerful and newer generations nawk and gawk do have better flexibility. They are widely used by fellow scripters due to its sheen power.
Let's look into some complex examples of AWK variants.
I would recommend using nawk format as its installed in most of the Unix systems (AIX, Linux, Ubuntu have nawk by default)
Syntax for one line awk commands
BEGIN { }, { } and end { }
An awk script can have three types of blocks.
One of them must be there.
a) The BEGIN{} block is processed before the file is checked.
b) The {} block runs for every line of input
c) The END{} block is processed after the final line of the input file.
String functions
#Multiple Field separator in Awk. use [][]. Below example will split whenever it finds "?>" in the data
awk -F'[?][>]' '{print $2}' /tmp/abc.out
Courtesy: http://www.well.ox.ac.uk/~johnb/comp/awk/awk.html
Let's look into some complex examples of AWK variants.
I would recommend using nawk format as its installed in most of the Unix systems (AIX, Linux, Ubuntu have nawk by default)
Syntax for one line awk commands
awk: awk -Fs '/search/ {action}' awkvar=$shellvar infile
nawk: awk -Fs -v awkvar=$shellvar '/search/ {action}' infile
gawk: awk -Fs -v awkvar=$shellvar '/search/ {action}' infile
BEGIN { }, { } and end { }
An awk script can have three types of blocks.
One of them must be there.
a) The BEGIN{} block is processed before the file is checked.
b) The {} block runs for every line of input
c) The END{} block is processed after the final line of the input file.
awk '
BEGIN { myvalue = 1000 }
/debt/ { myvalue -= $2 }
/want/ { myvalue += $4 }
END { print myvalue }
' inputFile
String functions
sub(regexp,sub) Substitute sub for regexp in $0
sub(regexp,sub,var) Substitute sub for regexp in var
gsub(regexp,sub) Globally substitute sub for regexp in $0
gsub(regexp,sub,var) Globally substitute sub for regexp in var
split(var,arr) Split var on white space into arr
split(var,arr,sep) Split var on white space into arr on sep as separator
index(bigvar,smallvar) Find index of smallvar in bigvar
match(bigvar,expr) Find index for regexp in bigvar
length(var) Number of characters in var
substr(var,num) Extract chars from posistion num to end
substr(var,num1,num2) Extract chars from num1 through num2
sprintf(format,vars) Format vars to a string
#Multiple Field separator in Awk. use [][]. Below example will split whenever it finds "?>" in the data
awk -F'[?][>]' '{print $2}' /tmp/abc.out
Courtesy: http://www.well.ox.ac.uk/~johnb/comp/awk/awk.html