awk

Extract lines after string and print multiple values in single line


I have several files with a format like this

some text
some text
This section is for WXYZ
some text
some text
some text
some text
some text
some text (ABC) some text (CDF)
901 98
some text FFG
some text (FFG)
1 99
some text
some text

I'm trying to print for each file

  1. the filename
  2. the string after "This section is for " in same line
  3. the line below the string containing (ABC)
  4. the line below the string containing (FFG)

This is my current script (based on the answer in this thread)

awk '/This section is for/{sub(/This section is for /,""); print FILENAME "|" $0}
     a{print;a=0} /\(ABC\)/{a=1}
     b{print;b=0} /\(FFG\)/{b=1}
' "testfile.txt"

I'm getting this output

testfile.txt|WXYZ
901 98
1 99

And my desired output for each file would be a single line like this

testfile.txt|WXYZ|901 98|1 99

How to modify the script to get my goal? Thanks

UPDATE

After processing all the files I have, I found that some outputs were wrong since some files have the line some text Code of FFG after the line some text (FFG), then if I use (FFG) as matching string, the output is some text Code of FFG instead of 1 99. Then I need to use Code of FFG as matching string for the second case and it seems to work in all cases.

some text
This section is for WXYZ
some text
some text
some text (ABC) some text (CDF)
901 98
some text (FFG)
some text Code of FFG
1 99
some text

Solution

  • Like this, using printf "%s" to avoid newlines:

    $ awk '/This section is for/{sub(/This section is for /,""); printf "%s", FILENAME "|" $0}
         a{printf "|%s", $0;a=0} /\(ABC\)/{a=1}
         b{printf "|%s\n", $0;b=0} /\(FFG\)/{b=1}
    ' testfile.txt
    testfile.txt|WXYZ|901 98|1 99