LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-30-2011, 03:37 PM   #1
rockie321
LQ Newbie
 
Registered: Mar 2011
Posts: 19

Rep: Reputation: 1
sed to display the pattern string, the line above it and the first line of that para


I need to grep for a particular string and if found need to display the line containing that string, the line above that and also the first line of that paragraph.

Can this be done via sed.
Thanks,

Eg, My Paragraphs

OA connectA
Enclosure:
Status: OK
Unit Identification LED: Off
Onboard Administrator:
Status: Critical
Power Subsystem:
Status: OK
Cooling Subsystem:
Status: OK

OA connectB
Interconnect Module #1 Status:
Health LED: OK
Interconnect Module #2 Status:
Health LED: Degraded
Interconnect Module #5 Status:
Health LED: OK
Interconnect Module #6 Status:


Here, if I grep for Critical, it should display the following

OA connectA <- The first line of the paragraph
Onboard Administrator: <- The line above the pattern string
Status: Critical <- The line containing the pattern string

similarly if I grep for Degraded, it should display

OA ConnectB
Interconnect Module #2 Status:
Health LED: Degraded
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-31-2011, 01:40 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Probably awk is better suited. Something like:
Code:
awk 'BEGIN{RS="";OFS=FS="\n"}/Critical|Degraded/{print $1;for(x=1;x<=NF;x++)if($x ~ /Critical|Degraded/)print $(x-1),$x;print ""}' file
You may want to use a variable in place of the regular expression so you can pass in what you like. I'll let you figure out that part
 
1 members found this post helpful.
Old 03-31-2011, 06:35 AM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
For each paragraph this stores the first line in the hold space then has a sliding window of two lines in the pattern space.
Code:
sed -n '/./{
h           # Copy the first line into the hold space
:a
N           # Append next line to create a two line pattern space
/\n.*Critical/{x;p;x;p}  # Print if a match in the most recent line
s/.*\n//    # Remove the oldest of the two lines
/./ba       # If the remaining line is not empty go to :a
}' infile > outfile

Last edited by Kenhelm; 03-31-2011 at 06:46 AM.
 
2 members found this post helpful.
Old 04-03-2011, 02:48 PM   #4
rockie321
LQ Newbie
 
Registered: Mar 2011
Posts: 19

Original Poster
Rep: Reputation: 1
Smile

Thanks a lot to Kenhelm and grail. Got exactly what I needed.
 
  


Reply

Tags
awk, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
[SOLVED] sed: delete last line matching a pattern colucix Programming 3 03-27-2011 01:00 PM
[SOLVED] Need sed help: s/ command won't replace two occurrences of pattern on same line GrapefruiTgirl Programming 7 12-16-2009 02:08 AM
replace a pattern with a line using sed/awk lokeshn05 Linux - Newbie 3 05-06-2009 03:01 PM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 01:47 PM


All times are GMT -5. The time now is 03:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration