Complex Filter using grep, awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Complex Filter using grep, awk or sed
# 1  
Old 08-29-2016
Linux 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

Code:
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

Code:
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
3. Line with DETAILS pattern, only when in lines above the pattern WARNING exists. The number of the lines with DETAILY can vary.

Thanks
# 2  
Old 08-29-2016
Try

Code:
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)

# 3  
Old 08-29-2016
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.
# 4  
Old 08-29-2016
Are there dashes at least?

If so, please try to edit Rudic's command to suit that pattern.
# 5  
Old 08-29-2016
Quote:
Originally Posted by Frankg
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.
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 Smilie
This User Gave Thanks to shamrock For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or sed or grep filter a line and/or between strings

Hi, I have multiple files on a directory with the following content: blahblah blahblah hostname server1 blahblah blahblah ---BEGIN--- aaa bbb ccc ddd ---END--- blahblah blahblah blahblah I would like to filter all the files with awk or sed or something else so I can get below... (6 Replies)
Discussion started by: bayupw
6 Replies

2. Shell Programming and Scripting

Awk/sed/cut to filter out records from a file based on criteria

I have two files and would need to filter out records based on certain criteria, these column are of variable lengths, but the lengths are uniform throughout all the records of the file. I have shown a sample of three records below. Line 1-9 is the item number "0227546_1" in the case of the first... (15 Replies)
Discussion started by: MIA651
15 Replies

3. Programming

How to replace the complex strings from a file using sed or awk?

Dear All, I am having a requirement to find the difference between 2 files and generate a discrepancy report out of it as an html page. I prefer using diff -y file1 file2 since it gives user friendly layout to know any discrepancy in the record and unique records among the 2 file. Here's how it... (12 Replies)
Discussion started by: Badhrish
12 Replies

4. Shell Programming and Scripting

Use of awk or sed to filter out the ouput

Hi, i am trying to get the system model with the help of awk : $ prtconf | awk '/^System Model/' System Model: IBM,8408-E8D but i want just the below outout that is command should chk for pattern <IBM,> and remove it from the final output : System Model:8408-E8D Can... (2 Replies)
Discussion started by: omkar.jadhav
2 Replies

5. Shell Programming and Scripting

Use of awk/sed to filter out fdisk output

Hi , I am trying to filter out the below output of fdisk -l command : fdisk -l Disk /dev/sda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 ... (9 Replies)
Discussion started by: omkar.jadhav
9 Replies

6. Shell Programming and Scripting

Complex string operation (awk, sed, other?)

I have a file that contains RewriteRules for 200 countries (2 examples for 1 country below): RewriteRule ^/at(/|/index.html|)$ http://%{HTTP_HOST}/locate/index.html?locale=de_AT #& RewriteRule ^/at_english(/|/index.html|)$ http://%{HTTP_HOST}/locate/index.html?locale=en_AT I have... (5 Replies)
Discussion started by: usshadowop
5 Replies

7. Shell Programming and Scripting

Need to build a grep/sed/awk filter

Hi I need to to direct only the path and the name of the trace file to a new file. How do I use grep/awk/sed filter? eg. ABC.root>cat alert_omc_dg.log | grep trc ORA-00060: Deadlock detected. More info in file /u01/oradata/omc/udump/omc_dg_ora_3555.trc. ORA-00060: Deadlock detected. More... (8 Replies)
Discussion started by: geetap
8 Replies

8. Shell Programming and Scripting

Complex find grep or sed command

Haven't worked in bash for ages. did a good bit of shell scripting in regular sh, but have forgotten most of it. I have several thousand php files that now include the following line at the end of the file. There is no LF or CR/LF before it begins, it is just concatenated to the final line of... (3 Replies)
Discussion started by: sjburden
3 Replies

9. Shell Programming and Scripting

Complex Sed/Awk Question?

Hello, So i have this file called /apps/turnout which looks like that of the contents of the /etc/shadow (but not exactly) the file has a long list in it. basically, the contents of this file looks something similar to the following: jajajajalala:D#$#AFVAdfda lalabavisof:#%R@fafla#$... (3 Replies)
Discussion started by: SkySmart
3 Replies

10. Shell Programming and Scripting

filter parts of a big file using awk or sed script

I need an assistance in file generation using awk, sed or anything... I have a big file that i need to filter desired parts only. The objective is to select (and print) the report # having the string "apple" on 2 consecutive lines in every report. Please note that the "apple" line has a HEX... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question