Tuesday, 19 August 2008

awk: first, last and count of a given string

This script is a modification of the code used in the previous post. Below is the code for an awk shell wrapper script (awkscript) that finds the first, last and count of occurrences for a given string. The script would be run on the command line like this:

$ awkscript input.file string

Here's the script (actually a shell script remember and not a pure awk script):

awk -F, '

/'"$2"'/ {
if ( min == "" ) {
min = $0
line = NR
lines[last] = $0
lastline = NR
print " "
print "First Occurrence: " line
print " "
print min
print " "
print "Last Occurrence: " lastline
print " "
print lines[last]
print " "
print "Total Matches: "total
print " "
}' $1

The interesting thing to notice with this script is how we pass the shell variable $2 (not to be confused with the awk variable $2) to the awk command. The standard awk string comparison test '//' has to have the protection of single then double quote protection around the shell variable.

No comments: