LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-15-2011, 10:02 AM   #1
csegau
LQ Newbie
 
Registered: Oct 2009
Posts: 27

Rep: Reputation: 0
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

Last edited by csegau; 05-15-2011 at 10:21 AM.
 
Old 05-15-2011, 10:12 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
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

Last edited by EricTRA; 05-15-2011 at 10:14 AM.
 
Old 05-15-2011, 10:13 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Code:
sed '/text1/,/text2/d' infile
...
jkl
mno
...
You can set a range (/text1/,/text2/) in sed.

Hope this helps.
 
Old 05-15-2011, 10:23 AM   #4
csegau
LQ Newbie
 
Registered: Oct 2009
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
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
 
Old 05-15-2011, 11:10 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
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
 
Old 05-15-2011, 11:21 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by csegau View Post
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.

Last edited by druuna; 05-15-2011 at 11:27 AM. Reason: grammar
 
Old 05-15-2011, 11:34 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Nominal Animal View Post
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...

Last edited by MTK358; 05-15-2011 at 11:38 AM.
 
Old 05-15-2011, 11:36 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by csegau View Post
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?
 
Old 05-15-2011, 11:39 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@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).
 
Old 05-15-2011, 11:44 AM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by druuna View Post
@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 View Post
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.

Last edited by MTK358; 05-15-2011 at 11:45 AM.
 
Old 05-15-2011, 11:50 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@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

Last edited by druuna; 05-15-2011 at 11:51 AM.
 
Old 05-15-2011, 11:57 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by druuna View Post
@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.
 
Old 05-15-2011, 07:09 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 05-16-2011, 09:55 AM   #14
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by grail View Post
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.
 
Old 05-16-2011, 10:22 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to copy some lines in a file and delete these lines after gartura Linux - General 1 07-20-2010 08:55 AM
How to use sed to delete all lines before the first match of a pattern? C_Blade Linux - Newbie 9 05-01-2010 04:18 AM
[SOLVED] Delete all lines containing a string, plus 4 lines below it? RedHelix Linux - Newbie 4 01-27-2010 09:13 AM
Delete Duplicate Lines in a file, leaving only the unique lines left xmrkite Linux - Software 6 01-14-2010 06:18 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration