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 |
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 |
Hi,
Code:
sed '/text1/,/text2/d' infile Hope this helps. |
Quote:
i want to remove text between text1 and text2 not between text2 and text1 |
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 Code:
awk '/text1/{s=1;next} /text2/{s=0;next} (!s)' infile |
Hi,
Quote:
Code:
sed '/text1/,/text2/d' infile 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 Anyway, hope this helps. |
Quote:
sed -n '/text1/,/text2/p' EDIT: maybe this isn't right, I got really confused, I'll read the thread again... |
Quote:
|
@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).
|
Quote:
But still, no matter how this comment: Quote:
|
@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 :) |
Quote:
|
Well not a sed guru, but this seems to work:
Code:
sed -n -e '/text[12]/p' -e '/text1/,/text2/d;p' file Code:
awk '/text1/,/text2/{if(/text[12]/)print;next}1' file |
Quote:
Could you please explain these codes. Thanks in advance. |
Hi vikas
Basically they are doing the same thing: Code:
sed -n -e '/text[12]/p' -e '/text1/,/text2/d;p' file 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 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. |