Ordinarily, I stick with what is installed with OS X. This awk example uses GNU awk (gawk), and I have installed homebrew (brew) package manager on my OS X El Capitan, and then used brew install gawk to put it into /usr/local/bin.
Although awk on OS X accepts the ENDFILE syntax without error, it does nothing when the end of an individual file is reached. It does work in gawk though.
Here is the awk script that you run in the Terminal, and provide *.txt files on the command line. It will process every line in these files looking for Deet, and when it reaches the end of the given file (ENDFILE), it will print out the last captured DEET, and the current filename. I called the following script deet.awk, and you run it as:
./deet.awk *.txt
#!/usr/local/bin/gawk -f
#
# capture and print the last match for word in its file
function usage()
{
printf "Usage: %s *.txt\n", ENVIRON["_"]
}
BEGIN {
if (ARGC < 2) {
usage()
exit 1
}
}
/Deet/ { match($0, /(Deet)/, z);a = z[1];b=FILENAME }
ENDFILE { printf "%s\t%s\n", a, b }
END { exit }
Output:
odin: ~/awkism$ ./deet.awk *.txt
Deet test10.txt
Deet test25.txt
Deet test30.txt
Deet test45.txt