sed to conditionally delete multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to conditionally delete multiple lines
# 1  
Old 06-22-2011
sed to conditionally delete multiple lines

Hello

I'd like to remove any line in an output file that is preceded by one or more warning messages (each warning is on a separate line).

Example :
Code:
WARNING: Estimation of lower confidence limit of \rho failed; setting it to 0.
822     28447                                                                   1.51e+05        0.00e+00<-8.66e-04<9.52e-03
WARNING: Estimation of lower confidence limit of \rho failed; setting it to 0.
823     28343                                                                   1.50e+05        0.00e+00<-8.82e-04<1.97e-02
WARNING: Estimation of lower confidence limit of \rho failed; setting it to 0.
WARNING: Estimation of upper confidence limit of \rho failed; setting it to 1.
824     28246                                                                   1.50e+05        0.00e+00<-5.84e-04<1.21e-03

All these lines should be deleted ; there can be more than 2 warnings, in which case the output line should be deleted as well.

A command like
Code:
sed '/WARNING/{N; //d}' SlFf.rho2

doesn't work for even numbers of warning messages.

I'm not familiar enough to the n, N, d, and D commands to figure out their behaviour. Any ideas ?

Thanks
jos
# 2  
Old 06-22-2011
an awk version:

Code:
 
awk '/WARNING/ {c=1;next} c==1 { c=0;next} 1' input_file
aa
bb
dd
dd
ee

# 3  
Old 06-22-2011
Code:
$ cat <<EOF | awk '/WARNING/ { W=2; } {} ((W--)<=0)'

WARNING
WARNING
DANGER WILL ROBINSON
DANGER DANGER
WARNING
THIS IS NOT A DRILL
THIS IS NOT A DRILL
THIS IS NOT A DRILL
THIS IS NOT A DRILL
EOF

DANGER DANGER
THIS IS NOT A DRILL
THIS IS NOT A DRILL
THIS IS NOT A DRILL
$

Every line that matches WARNING sets W=2. Every line, whether it matches or not, decrements W. So a matching line in effect sets it to 1. The statement at the end of the {} prevents printing when W > 0, which avoids one or more warning lines in a row plus one line after. You could change W=2 to W=3 if you wanted to skip two lines after.
These 2 Users Gave Thanks to Corona688 For This Post:
# 4  
Old 06-22-2011
Great, thanks !
It would definitely have taken a lot of time before I would have figured out that one can use awk this way ...
# 5  
Old 06-22-2011
Thank you, Corona688. Your sample data made me laugh.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

2. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

3. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

4. Shell Programming and Scripting

Conditionally delete last X lines

delete last X lines,which start with + example file: test1 test2 remove1 remove2 one liner shell is preferred. (8 Replies)
Discussion started by: honglus
8 Replies

5. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 Replies

6. UNIX for Dummies Questions & Answers

Conditionally joining lines in vi

I've done this before but I can't remember how. Too long away from vi. I want to do a search are replace, but I want the replace to be a join. Example see spot run see spot walk see spot run fast see spot hop %s/run$/<somehow perform a join with the next line>/g so the results... (0 Replies)
Discussion started by: ifermon
0 Replies

7. Shell Programming and Scripting

using sed command to delete a string spanning multiple lines

file1 contains the following data sssssssssss firstline secondline pppppppppp ssssssssss Using sed comamnd i am trying to delete firtsline secondline. so, output should be sssssssssss pppppppppp ssssssssss I tried in the following the way, but it is not working. sed ... (9 Replies)
Discussion started by: radha.kalivar
9 Replies

8. UNIX for Dummies Questions & Answers

Delete multiple lines containting a variable string using SED.

Good morning, Novice scripter in Unix here, and I've run into and sed task I can't quite wrap my head around. I'm pulling my hair out fast enough as it is and thought I would go to the knowledge bank. I have a sorted file that I'm trying to trim down by deleting any line whose first few... (2 Replies)
Discussion started by: selkirk
2 Replies

9. Shell Programming and Scripting

delete multiple empty lines

Hi, I need to delete the lines with empty name. What is the best way to do it? Thanks a lot for your help! EMID MMDDYY HOURS JOB EMNAME 0241 051605 11.40 52062 someone 0520 051605 10.63 52062 0520 051605 10.66 52062 0520 051605 10.65 52062 (3 Replies)
Discussion started by: whatisthis
3 Replies

10. Shell Programming and Scripting

Delete multiple lines w/ sed

Hi all, I am trying to figure out the syntx to delete multiple lines w/ sed. I know the following syntax will delete lines 1 THROUGH 5 from filex: sed 1,5d filex But I wan to delete lines 1 AND 5 (keeping lines 2,3, and 4). Does anyone know how to do this in a single sed statement? ... (2 Replies)
Discussion started by: bookoo
2 Replies
Login or Register to Ask a Question