LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   delete lines between match (https://www.linuxquestions.org/questions/programming-9/delete-lines-between-match-880834/)

csegau 05-15-2011 10:02 AM

delete lines between match
 
Hi All,

My problem is like this

I have to delete all lines between two pattern match
example- suppose below is the content of the file
then i have to delete all lines between text1 and text2
...
text1
abc
def
ghi
text2
jkl
mno
text1
pqr
xyz
text2
...

I only want to remove text between text1 and text2
so the output i want is like this

text1
text2
jkl
mno
text1
text2


Thanks in advance

EricTRA 05-15-2011 10:12 AM

Hello,

If I understand you correctly then you want to delete all lines except the ones that contain jkl and mno? Regardless of what's in the two blocks between text1 and text2?
Kind regards,

Eric

druuna 05-15-2011 10:13 AM

Hi,

Code:

sed '/text1/,/text2/d' infile
...
jkl
mno
...

You can set a range (/text1/,/text2/) in sed.

Hope this helps.

csegau 05-15-2011 10:23 AM

Quote:

Originally Posted by druuna (Post 4356918)
Hi,

Code:

sed '/text1/,/text2/d' infile
...
jkl
mno
...

You can set a range (/text1/,/text2/) in sed.

Hope this helps.


i want to remove text between text1 and text2 not between text2 and text1

Nominal Animal 05-15-2011 11:10 AM

There is most likely a sed stanza that does this, but with awk,
Code:

awk '(!s){print} /text1/{s=1} /text2/{if(s)print;s=0}' infile
should work. If you wish to omit the text1 and text2 lines, then
Code:

awk '/text1/{s=1;next} /text2/{s=0;next} (!s)' infile

druuna 05-15-2011 11:21 AM

Hi,
Quote:

Originally Posted by csegau (Post 4356923)
i want to remove text between text1 and text2 not between text2 and text1

Code:

sed '/text1/,/text2/d' infile
This does print what's between text1 and text2, what it doesn't do is print the search tokens :)

If you want the tokens you search for be part of the output things get a bit more complicated. Have a look at this:
Code:

$ cat foo.sh
#!/bin/bash

awk 'BEGIN { seent1 = 0 }
/text1/ { seent1 = 1 ; print }
{ if ( seent1 == 0 ) { print } }
/text2/ { seent1 = 0 ; print }
' infile

$ ./foo.sh
...
text1
text2
jkl
mno
text1
text2
...

$ cat infile
...
text1
abc
def
ghi
text2
jkl
mno
text1
pqr
xyz
text2
...

I just noticed Nominal Animal's reply: Maybe better (at least shorter and a more manageable one-liner).

Anyway, hope this helps.

MTK358 05-15-2011 11:34 AM

Quote:

Originally Posted by Nominal Animal (Post 4356962)
There is most likely a sed stanza that does this

This deletes everything outside of text1 and text2, and does not delete the delimiters:

sed -n '/text1/,/text2/p'

EDIT: maybe this isn't right, I got really confused, I'll read the thread again...

MTK358 05-15-2011 11:36 AM

Quote:

Originally Posted by csegau (Post 4356923)
i want to remove text between text1 and text2

That's exactly what druuna's sed command does. Maybe you meant you want to leave alone the text between text1 and text2?

druuna 05-15-2011 11:39 AM

@MTK358: My solution does do that, but the OP wants the search tokens to be shown as well (posts #5 and #6 tackle that issue).

MTK358 05-15-2011 11:44 AM

Quote:

Originally Posted by druuna (Post 4356995)
@MTK358: My solution does do that, but the OP wants the search tokens to be shown as well (posts #5 and #6 tackle that issue).

I don't know how to do that with sed.

But still, no matter how this comment:

Quote:

Originally Posted by csegau (Post 4356923)
i want to remove text between text1 and text2 not between text2 and text1

could mean that. Your sed command did not delete the text between text2 and text1.

druuna 05-15-2011 11:50 AM

@MTK358: Take a look at the (edited) in and out examples the OP posted (post #1) and my original output in post #4. I'm assuming/guessing the OP did not fully understand the command I used when (re-)describing the problem it generated.

BTW: I'm also not aware of a sed solution (there's probably one, but the awk solutions posted are the better way to go).

Lets wait and see what the OP has to say :)

MTK358 05-15-2011 11:57 AM

Quote:

Originally Posted by druuna (Post 4357011)
@MTK358: Take a look at the (edited) in and out examples the OP posted (post #1) and my original output in post #4. I'm assuming/guessing the OP did not fully understand the command I used when (re-)describing the problem it generated.

It acted exactly like the OP wanted, except it removed the delimiters. It did not delete between the wrong delimiters, as the OP's post claimed.

grail 05-15-2011 07:09 PM

Well not a sed guru, but this seems to work:
Code:

sed -n -e '/text[12]/p' -e '/text1/,/text2/d;p' file
And same idea in awk:
Code:

awk '/text1/,/text2/{if(/text[12]/)print;next}1' file

vikas027 05-16-2011 09:55 AM

Quote:

Originally Posted by grail (Post 4357335)
Well not a sed guru, but this seems to work:
Code:

sed -n -e '/text[12]/p' -e '/text1/,/text2/d;p' file
And same idea in awk:
Code:

awk '/text1/,/text2/{if(/text[12]/)print;next}1' file

Hi Grail,

Could you please explain these codes. Thanks in advance.

grail 05-16-2011 10:22 AM

Hi vikas

Basically they are doing the same thing:
Code:

sed -n -e '/text[12]/p' -e '/text1/,/text2/d;p' file
1. '/text[12]/p' - print any lines containing the string 'text1' or 'text2'

2. '/text1/,/text2/d;p' - delete anything from text1 to text2 inclusive and print the rest
Code:

awk '/text1/,/text2/{if(/text[12]/)print;next}1' file
1. /text1/,/text2/ - search between text1 and text2 inclusive, when true go to step 2

2. if(/text[12]/)print;next - if line contains strings 'text1' or 'text2' then print. Whether previous is true or not go to the next record

3. 1 - print all lines not in the boundary test from step 1

Hope that helps :)


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