-1

How can I just print the pattern blocks where I have DFW-YYYY... and ignore the others. My start Pattern is ### My Ending Pattern is ##

Also the line pattern DFW-YYYY can have multi [] brackets. it is not fixed. I need to match the exact/ partial string (which could involve [blah] brackets for the match.

I had seen the article where the whole pattern is brought into a buffer and only printed when end pattern is found. I am looking for an enhancement to that thought process that I print conditionally once I find the start and end pattern block. The article I am talking about is: How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)?

I have a file like: 
xxx
xxx
###
xxx
zzz Problem Key: DFW-XXXXX [blah blah][blah blah][blah blah]
yyy
xxx
*##*
yyy
###
 zzz 
xxx 
yyy 
zzz Problem Key: DFW-YYYY [blah blah][blah blah]
*##* 
xxx 
xxx 
zzz 
yyy
### 
xxx 
zzz Problem Key: DFW-XXXXX [blah blah][blah blah][blah blah]  
yyy 
xxx
*##* ....

The pattern I was trying to enhance was:

awk 'flag{
        if (/*##*/)
           if  (print_flag =1 )
               {printf "%s", buf; flag=0; buf=""} /* only print if print_flag =1 */
           else 
               {  flag=0; buf=""} /* ignore pattern */
        else
            buf = buf $0 ORS

            /* peek into $0 and look for conditional pattern. 
              conditional pattern may have sq brackets like [] but not limited to one occurrence
              I could avoid spl chars if I could look for multiple small strings within $0.
              Like $0 ~ "x" and $0 ~ "y" and $0 ~ "z", this way spl. char matching can be avoided
              but conditional pattern can still be matched*/

              if ($0 ~ "conditional-pattern1" and $0 ~ "conditional-pattern2" and $0 !~ "conditional-pattern3")
                  print_flag=1
     }
     /###/ {flag=1}' <file-name>

But my if statements of looking into $0 value for condition and assigning/ checking print_flag is failing syntactically.

Any guidance on how this could be achieved.
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
Sanjay S
  • 1
  • 1
  • awk isn't C, it doesn't use `/* ... */` for comments, it uses `# to end of line` like shell. Telling us the exact error message you're getting including the line number it tells you the problem is on would be far more useful than just saying `is failing syntactically`. If you still have a problem after fixing the syntax of your comments then please update your question with the new code and exact error message or other problem description. – Ed Morton May 03 '20 at 21:30

2 Answers2

0

I got it working for me as:

 awk 'flag{
    if (/*##*/)
       {printf "%s", buf ~ "conditional-pattern" ? buf : "" ; flag=0; buf=""}
    else
        buf = buf $0 ORS
 }
  /###/{flag=1} ' file.txt

Multiple conditional pattern matching was achieved with:

{printf "%s", buf ~ "aaaaaa" && buf ~ "xxxxxxx" ? buf : "" ; flag=0; buf=""}

Sanjay S
  • 1
  • 1
0

with gawk multi-char record separator, something like this should work

$ awk -v RS='\n(*##*|###)' '!(NR%2) && /DFW-YYYY/' file


 zzz
xxx
yyy
zzz Problem Key: DFW-YYYY [blah blah][blah blah]

test with your actual pattern you're looking for.

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • 1
    Remember `*` is a regexp metachar so `*##*` isn't literal in a RS (and in fact the first `*` is undefined behavior as used since as the first char in a RE segment it's a repeat of nothing). – Ed Morton May 04 '20 at 01:35