Sunday, October 30, 2011

match() funtion in awk has a 3rd argument

Just came to know that the function match() in awk has a third argument!
The third argument is an array where you can capture the search results.

For eg:-

Code:
root@bt > cat infile
ajith & ajeesh
root@bt> awk '{match($0,"([a-z]*) & ([a-z]*)",arr); print arr[1]" : "arr[2] }' infile
ajith : ajeesh

([a-z]*) => will capture the first element of the array
&
([a-z]*) => will capture the second element of the array

And the array name here is "arr"