How to make 'awk' refer to other lines?

Hello Forum

A couple days ago I got some great help to come up with the following scrip which I could have never ever created:


on run
set theFileAsPOSIX to POSIX path of (choose file with prompt "Choose a file whose screen frequencies and angles you wish to display.")
set screenFrequenciesAndAngles to (do shell script "/usr/bin/strings" & space & quoted form of theFileAsPOSIX & "| /usr/bin/awk '/getspotfunction setscreen/{print \"Frequency: \"$1\" Angle: \"$2}'")
display alert "The following screen frequencies and angles were found in the chosen colour plate:" message screenFrequenciesAndAngles
end run

Can awk as well return a line which is, let’s say 4 lines above or 2 lines below the found line incl. “getspotfunction setscreen”?
I am working on expanding the script to include as well the plate-name which is found 4 lines above the “getspotfunction setscreen” info.

Thanks,

Axel

Browser: Safari 416.12
Operating System: Mac OS X (10.4)

Awk is practically a progamming language itself, so you can pull anything out you want out of a string. Can you show us the relevant output of the strings function? Is there a lable you can use to find the line you’re looking for (like you’re using “getspotfunction setscreen”)? If not, we can write a regular expression that will pull out a line 4 lines before the one where you find screen and angle info.

hmm…interesting question - I don’t think your going to be able to do this with awk - as you’re essentially jusy using it like grep - that is - to find a particular string.

what you might try is to grep for “getspotfunction setscreen” and then - based on the line number that matches - subtract 2 from it and then read that line - or add 4 to it and read that…etc…

eg:
you have a file callled text.txt with the following contents:

this is a test
this red boat is a test
this is a test
this is a test
this is a test
this red boat a test
this is a test

if you type this in the terminal:
grep -n “red boat” /path/to/file/text.txt

you’ll get (see the line numbers?)
2:this red boat is a test
6:this red boat a test

So - I’m thinking you could use them to get the other lines you need…
to print the specific line… let’s say it’s line # is 6:

head -6 /path/to/file/text.txt | tail -1

hope this helps.

You can also use these options for grep:
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing – between contiguous groups of
matches.

   -B NUM, --before-context=NUM
          Print  NUM  lines  of  leading  context  before  matching lines.
          Places  a  line  containing  --  between  contiguous  groups  of
          matches.

So for the example file above, you would get
$ grep -A 1 -B 1 red file.txt
this is a test
this red boat is a test
this is a test

this is a test
this red boat a test
this is a test

Sorry, I didn’t see your e-mail to me and forgot to check up on the other thread this question came from. Awk can definitely do this.

Four lines above the found regex:

Two lines above:

(I have added spaces around the /regex/ to make it more readable.)