Advanced grep and sed

I am wondering if there is a way via grep and sed to extract a string that is on the 2nd line below a known marker as in this example:

TextRel 203 0 0 "WELL:"
SetPosAbs 1287 -6676
TextRel 210 0 0 "AEP #2"

The marker is WELL:, but the string I need is "AEP #2". Can grep/sed handle this or is some sort of read command required?

Thanks,

Paul H.
Denver

nawk -v qq='"' 'c&&!--c {print substr($0, index($0,qq))};$NF ~ "WELL:" {c=2;next}' myFile

I used your code in the following string:

Set Well = `nawk -v qq='"' 'c&&!--c {print substr($0, index($0,qq))};$NF ~ "WELL:" {c=2;next}' $file`
echo $Well

I got the following message:

1: Event not found

Perhaps I should have added that I am using Cshell and that the 3 data lines shown in my original post are part of a larger file. The string "WELL:" however is the first instance of that string in the file. Hope that doesn't make it more difficult.

Thanks,
Paul H.

it should have worked regardless of the shell, but csh might be a different story.
Try executing from the command line and see what it gives you.

Using the following command at the command line prompt:

nawk -v qq='"' 'c&&!--c {print substr($0, index($0,qq))};$NF ~ "WELL:" {c=2;next}' asciipds

I got the following message:

123: Event not found

I know that the file asciipds is present and that it does contain the string "WELL:" as shown in my original post. Perhaps it is the csh after all.

Thanks,
Paul H.

Another way:

awk '/"WELL:"/{getline;getline;print $5, $6; exit}' asciipds

Regards

That works both at the prompt and from within the script. Thanks Loads,

Paul H.

Most likely you have embedded spaces in your 'WELL:' string.
here's the data file I've been testing with:

TextRel 203 0 0 "WELL:"
SetPosAbs 1287 -6676
TextRel 210 0 0 "AEP #2"

If you still care, just post your data file using the BB codes

The actual file is immense, so I'll just add a few lines of particular interest to me. There is a problem reading other lines where the number of parameters varies as in the following:

TextRel 217 0 0 "COMPANY: "
SetPosAbs 1287 -6656
TextRel 224 0 0 "BATTELLE MEMORIAL INSTITUTE"

In this case I only get "BATTELLE MEMORIAL" returned. The string that I need to retrieve in each case will always be enclosed in double quotes at the end of the second line below the line containing the marker. Within those quotes, there may be any number of spaces. Sorry I didn't clarify that earlier. Is that still doable?

THanks,
Paul H.

nawk -v qq='"' 'c&&!--c {print substr($0, index($0,qq))};/"[ ]*COMPANY:[ ]*"/ {c=2;next}' myFile

Or:

awk -F "\"" '/COMPANY/{getline;getline;print $2}' file

Regards

Another one

Despite trying various modifications of the awk command I was not able to get it to work.
The sed command came very close though. It returned the desired string, but also a second undesired line. My input lines of interest are:

TextRel 217 0 0 "COMPANY: "
SetPosAbs 1287 -6656
TextRel 224 0 0 "BATTELLE MEMORIAL INSTITUTE"
SetPosAbs 1137 -6680
TextRel 218 0 0 "WELL:"
SetPosAbs 1287 -6626
TextRel 225 0 0 "AEPAEP"
SetPosAbs 1141 -6596
TextRel 219 0 0 "FIELD:"
SetPosAbs 1287 -6596
TextRel 226 0 0 "MOUNTAINEER"
SetPosAbs 1141 -6566
TextRel 220 0 0 "County: "
SetPosAbs 1287 -6566
TextRel 227 0 0 "MASON"
SetPosAbs 1141 -6536
TextRel 221 0 0 "State:"
SetPosAbs 1287 -6536
TextRel 228 0 0 "WEST VIRGINIA"
SetPosAbs 1141 -6506
TextRel 222 0 0 "COUNTRY:"
SetPosAbs 1287 -6506
TextRel 229 0 0 "USA"

I need to retrieve the string contained in quotes on the second line below the marker line. So for the above, I need to retrieve "AEPAEP" for WELL:, "MOUNTAINEER" for FIELD:, and "BATTELLE MEMORIAL INSTITUTE" for COMPANY:, etc, (minus the double quotes.)
The sed command returned the following:
AEPAEP
FIELD:
Thanks for everyone's help on this. While I can decipher most CShell code, sed and awk are from a different planetary system.
Thanks,
Paul H.

Despite trying various modifications of the awk command I was not able to get it to work.

The sed command came very close though. It returned the desired string, but also a second undesired line. My input lines of interest are:

TextRel 217 0 0 "COMPANY: "
SetPosAbs 1287 -6656
TextRel 224 0 0 "BATTELLE MEMORIAL INSTITUTE"
SetPosAbs 1137 -6680
TextRel 218 0 0 "WELL:"
SetPosAbs 1287 -6626
TextRel 225 0 0 "AEPAEP"
SetPosAbs 1141 -6596
TextRel 219 0 0 "FIELD:"
SetPosAbs 1287 -6596
TextRel 226 0 0 "MOUNTAINEER"
SetPosAbs 1141 -6566
TextRel 220 0 0 "County: "
SetPosAbs 1287 -6566
TextRel 227 0 0 "MASON"
SetPosAbs 1141 -6536
TextRel 221 0 0 "State:"
SetPosAbs 1287 -6536
TextRel 228 0 0 "WEST VIRGINIA"
SetPosAbs 1141 -6506
TextRel 222 0 0 "COUNTRY:"
SetPosAbs 1287 -6506
TextRel 229 0 0 "USA"

I need to retrieve the string contained in quotes on the second line below the marker line. So for the above, I need to retrieve "AEPAEP" for WELL:, "MOUNTAINEER" for FIELD:, and "BATTELLE MEMORIAL INSTITUTE" for COMPANY:, etc, (minus the double quotes.)

The sed command returned the following:

AEPAEP
FIELD:

Thanks for everyone's help on this. While I can decipher most CShell code, sed and awk are from a different planetary system.

Thanks,
Paul H.

I have just discovered that by piping the results of the sed command to head -1 returns the desired string, (and only the desired string). ie:

sed -n '/WELL:/{n;n;p;}' asciipds | awk -F\" '{ print $2 }' | head -1

Thanks,
Paul H.

Have you tried the awk solution?

awk -F "\"" '/COMPANY/{getline;getline;print $2}' file

This is the output I get:

$ cat file
TextRel 217 0 0 "COMPANY: "
SetPosAbs 1287 -6656
TextRel 224 0 0 "BATTELLE MEMORIAL INSTITUTE"
SetPosAbs 1137 -6680
TextRel 218 0 0 "WELL:"
SetPosAbs 1287 -6626
TextRel 225 0 0 "AEPAEP"
SetPosAbs 1141 -6596
TextRel 219 0 0 "FIELD:"
SetPosAbs 1287 -6596
TextRel 226 0 0 "MOUNTAINEER"
SetPosAbs 1141 -6566
TextRel 220 0 0 "County: "
SetPosAbs 1287 -6566
TextRel 227 0 0 "MASON"
SetPosAbs 1141 -6536
TextRel 221 0 0 "State:"
SetPosAbs 1287 -6536
TextRel 228 0 0 "WEST VIRGINIA"
SetPosAbs 1141 -6506
TextRel 222 0 0 "COUNTRY:"
SetPosAbs 1287 -6506
TextRel 229 0 0 "USA"
$
$ awk -F "\"" '/COMPANY/{getline;getline;print $2}' file
BATTELLE MEMORIAL INSTITUTE
$ awk -F "\"" '/WELL/{getline;getline;print $2}' file
AEPAEP
~/scripts$ awk -F "\"" '/FIELD/{getline;getline;print $2}' file
MOUNTAINEER
$ awk -F "\"" '/COMPANY/{getline;getline;print $2}' file
BATTELLE MEMORIAL INSTITUTE
$

Have I missed something?

Regards

When I run the following command from the command line prompt:

awk -F "\"" '/COMPANY/{getline;getline;print $2}' asciipds

I get:
Unmatched "

When I run:

awk -F \" '/COMPANY/{getline;getline;print $2}' asciipds

I get:

awk: can't open /COMPANY/{getline;getline;print $2}

Which shell are you running? I'm running in the csh which is what may be making the difference.

Thanks,
Paul H.

Try it with nawk or /usr/xpg4/bin/awk on Solaris.

Regards

The following command:

nawk -F \" '/COMPANY/{getline;getline;print $2}' asciipds | head -1

returned the following string:

BATTELLE MEMORIAL INSTITUTE

Without the "head -1" it was returning a second unwanted string as well.

Is nawk specifically for the c shell?

Thanks,
Paul H.

I don't think it should not matters but the c shell is not recommended for scripting:

http://www.grymoire.com/Unix/CshTop10.txt

Regards