Complex Filter using grep, awk or sed

Hi, I'm not very familiar witrh sed or awk and hope the somebody can help me to solve my problem. I need to filter a text report using grep, sed or awk. I would like to cut out text lines with the pattern INFO and if exists the following lines of the pattern DETAILS. I need te keep the lines with the pattern DETAIL if in the lines before the pattern WARNUNG exists. The number of line with pattern WARNUNG, INFO and DETAILS can varry. Is this possible? If yes, how is the syntax?

Input Textfile example

Heading text 1
------------------------------------
WARNING text 
WARNING text 
INFO text1
INFO text1
INFO text2
DETAILS text (Info)
DETAILS text (Info)
DETAILS text (Info)

Heading text 1
------------------------------------
INFO text1
WARNING text
WARNING text
DETAILS text (warning)
DETAILS text (warning)
WARNING text
DETAILS text (warning) 
DETAILS text (warning)
DETAILS text (warning)
                                
Heading text 1
------------------------------------
INFO text1
DETAILS text (Info)
DETAILS text (Info)
INFO  text (Info)
WARNING text 
DETAILS text (warning)

Output Needed

Heading text 1
------------------------------------
WARNING text 
WARNING text 

Heading text 1
------------------------------------
WARNING text
WARNING text
DETAILS text (warning)
DETAILS text (warning)
WARNING text
DETAILS text (warning) 
DETAILS text (warning)
DETAILS text (warning)
                                
Heading text 1
------------------------------------
WARNING text 
DETAILS text (warning)

In need to show only the following lines_

  1. Line with heading text
  2. Line with the underlining of the heading test
  3. Line with WARNING pattern
  4. Line with DETAILS pattern, only when in lines above the pattern WARNING exists. The number of the lines with DETAILY can vary.

Thanks

Try

awk '/^Head/ {print; getline; print; next}  /^WARN/ {P = 1} /^INFO/ {P = 0} P || !NF' file
Heading text 1
------------------------------------
WARNING text 
WARNING text 

Heading text 1
------------------------------------
WARNING text
WARNING text
DETAILS text (warning)
DETAILS text (warning)
WARNING text
DETAILS text (warning) 
DETAILS text (warning)
DETAILS text (warning)
                                
Heading text 1
------------------------------------
WARNING text 
DETAILS text (warning)

Hi RudiC, thank You for you suggestion. I have forgotten to mention that the header of each section is not a pattern, because it could be any value. So I guess your suggestion won't work. Sorry, my fault.

Are there dashes at least?

If so, please try to edit Rudic's command to suit that pattern.

Then why dont you post a sample of the exact input that is to be worked on...please dont expect us to read your mind :confused:

1 Like