(2001-07-31) SCAN.AWK - Scan for two strings in a file

This script will scan a file looking for a trigger string. If it finds that trigger string, it will scan the rest of the file looking for a pair of strings indicating a field and a value. The intention is that the first string (the trigger) indicates that the file is of the right type, and the pair of strings allow you to select the field of interest, and the value you want to see.

When the script finds a file containing the trigger string, followed by the field with the right value, it will output a command to copy the file. The idea is that you can run this script to find and copy all the files containing the trigger and the right value for the chosen field. The script also outputs debugging information (or progress information) as comments to the shell script. This will work in UNIX, but not in DOS. In DOS, you will have to disable the debugging lines.

This script is written with options for DOS or UNIX, and you can extend that to create a different command for your system. It assumes that multiple files matching the criteria should be appended to the output.

To run it in a UNIX environment, you would use a command like:

awk -f scan.awk trigger=aaa field=bbbb value=cccc * | /bin/sh
In DOS, you would have to use a command like:
awk -f scan.awk trigger=aaa field=bbbb value=cccc *.* > tmp.bat
then run tmp.bat to actually copy the files.

#File: scan.awk #?? SCAN.AWK # #?v Written 2001-03-24 #?v By Rob Ewan # # This is an AWK program to scan for files containing two triggers. It will # output a list of the files which it finds. # #? Usage: #? AWK -f scan.awk file(s) #? Options: #? trigger=string To specify the trigger sequence #? field=string To specify the field name of interest #? value=string To specify the field value of interest #? os=dos/unix To specify the operating system (DOS/UNIX) #? # Put initialization code here BEGIN { fileName = ""; # Set some default values trigger = "aaa"; field = "bbbb"; value = "cccc"; os = "unix"; } # When we see the next file fileName != FILENAME { # Set up processing for next file fileName = FILENAME; print "# Starting a new file: " fileName; # Have not found the trigger yet foundTrigger = 0; # Have not marked the file for copying yet markedFile = 0; } # If this line contains the trigger, start looking for field and value $0 ~ trigger { print "# Found trigger: " $0; foundTrigger = 1; } # If we found the trigger, and this line contains the field and value foundTrigger && $0 ~ field ": *" value { print "# Found field: " $0; # If we have not already marked this file for copying if (!markedFile) { # Generate OS-specific command to copy the contents if (os == "unix") { print "cat " fileName " >>" value "." trigger; } else if (os == "dos") { print "type " fileName " >>" value "." trigger; } else { print fileName; } # Mark the file to prevent copying it twice markedFile = 1; } } # Put the closing code here END { # Nothing to do }
- Read the book on AWK.
- Read more of my ideas on programming.
- Look up some other scripts.
- Go back to the front gate.

Do you have a script which I might find useful? Did you find a problem in one of my scripts? Write to Rob to let me know.
Page maintained by Rob.