A sed question 
Author Message
 A sed question

I have a situation with sed where I would like to delete
several lines of text based on the content of one of them.
My data comes as a stream of blocks in the form:

 header line
 <body line1>
 <body line2>
 ...
 <body line3>
 footer line

I would like to be able to delete the entire block
if any of the text within it matches a regexp

Is there an easy way to do it ?

I know I have to use the 'N' command, but I just can't
seem to get the thing to work

stuart

--
--
There are those who are born UNIX  |Stuart Yeates


For the kingdom of heaven's sake   |Matthew 19:12



Sun, 07 Jun 1998 03:00:00 GMT  
 A sed question

Quote:

>I have a situation with sed where I would like to delete
>several lines of text based on the content of one of them.
>My data comes as a stream of blocks in the form:
> header line
> <body line1>
> <body line2>
> ...
> <body line3>
> footer line
>I would like to be able to delete the entire block
>if any of the text within it matches a regexp
>Is there an easy way to do it ?

Sure. Save the following to a file called, say, "myscript."

/start/{N
N
N
N
/reg_exp/d

Quote:
}

where, /start/ is a regexp matching the header line, each "N" in the script
adds another line to the matched pattern (so there should be as many N's as
make up the body plus the footer line), /reg_exp/ is the regexp that you want
to consider for deciding whether to delete the pattern.

Use as,

   sed -f myscript file

Regards,
Gora



Sun, 07 Jun 1998 03:00:00 GMT  
 A sed question

Quote:

>I have a situation with sed where I would like to delete
>several lines of text based on the content of one of them.
>My data comes as a stream of blocks in the form:

> header line
> <body line1>
> <body line2>
> ...
> <body line3>
> footer line

>I would like to be able to delete the entire block
>if any of the text within it matches a regexp

>Is there an easy way to do it ?

>I know I have to use the 'N' command, but I just can't
>seem to get the thing to work

>stuart

>--
>--
>There are those who are born UNIX  |Stuart Yeates


>For the kingdom of heaven's sake   |Matthew 19:12

An awk script similar to the following should do:
/begin line/ {  ct = 0
                hold[ct] = $0
                kill = 0
                next
             }
/regexp/     {  kill = 1 }
             {  hold[++ct] = $0 }
/end line/   {  if(kill==0) {
                  for(i=0;i<ct;i++) print hold[i]
             }

This script presumes clean data; no consecutive begin lines, no
consecutive end lines, etc.
--
Bob Stearns
University of Georgia

(706)542-5110



Sun, 07 Jun 1998 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to do this.. (SED question)

2. a sed question

3. SED question

4. Sed Question

5. Urgent VI/SED question

6. This might be a sed question

7. sed question

8. A sed question

9. awk vs. sed question

10. sed question: unreliable "/^$/d" ?

11. Newbie awk (sed??) question, regular expressions

12. Awk/Sed Filehandler question

 

 
Powered by phpBB® Forum Software