views:

493

answers:

4

In a sh shell script.

Given data in a text file:
string1
string2 gibberish
gibberish
string3 gibberish
string4

How could you use awk or sed to remove all lines between string2(inclusive) and string3(not including string 3)?

to end up with:
string1
string3
string4

A: 

Here's a sample regex substitution:

s/string2.*?(?=string3)//sg

Which will remove everything from string2 up to but not including string3.

Anon.
I presume that's Perl, right? Cuz it doesn't look like either "awk or sed" (as the question states) to me. :-P
Chris Jester-Young
Yeah, and sed doesn't do non-greedy either.
David Kanarek
bad flag in substitute command: 's'Is that perl?
Eccen
+2  A: 

Are string1, string2,string3, etc. each on different lines? In that case, you can use awk:

awk '/string2/{flag=1} /string3/{flag=0} !flag'

or sed:

sed '/string3/p; /string2/,/string3/d'
marco
jsoverson
Those strings are on different lines. There's another word after each string that is dynamic.
Eccen
These totally worked if they were on lines by themselves. But, I realized soon after you asked that there was some words before or after the string that didn't need to be erased that were on the same line.
Eccen
Won't the above sed solution cause a doubling of a "string3" line is NOT preceded by a "string2" line?
Beano
You are right, but given the question of the OP, I made the assumption that input file always contains a line containing "string2" before the one containing "string3"
marco
+2  A: 

you can try this. Anything before "string2" will not be deleted.

awk 'BEGIN{f=0}
{
    match($0,"string2")
    if(RSTART){
        print substr($0,1,RSTART-1)
        f=1
        next
    }
    match($0,"string3")
    if(RSTART){
        $0=substr($0,RSTART)
        f=0
    }
}
f==0{print}
' file

output

$ cat file
string1 blah blah
text before string2 junk
gibberish
gibberis string3 text here
string4

$ ./shell.sh
string1 blah blah
text before
string3 text here
string4
ghostdog74
Your input and output look right. Testing...
Eccen
This is a comprehensive solution that works in my case. Thanks!
Eccen
A: 

The following will work in sed

sed  '
/string2/,/string3/bdeleting
b
:deleting
s/string3.*/string3/
/string3/b
d
'

presuming we are matching up to the first occurrence of string3 after string2

Beano