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 |
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 |
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 '/./{ |
Thanks a lot to Kenhelm and grail. Got exactly what I needed.
|
All times are GMT -5. The time now is 09:02 AM. |