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 11-19-2019, 08:20 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
Sed remove between patterns


This code is probably here already in the forum , but search engine did not poped up any results for what i need .

And what i need is pretty simple , use sed to remove everything between 2 patterns , but do not remove the patterns .

Example :


#starthere
dwfwef
efewf
wefewf
#endhere

Desired output

#starthere
#endhere


This next code removes also the patterns , but i dont want them to be removed , i want only what there is between them .
Code:
sed -i "/\#starthere/,/\#endhere/d" myfile
 
Old 11-19-2019, 08:30 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
http://www.grymoire.com/Unix/Sed.html

After the dozens of questions you have asked on Sed, you should have the above well book marked and be able to solve this
 
Old 11-19-2019, 08:34 AM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
By default, sed works on one line at a time.
There are patterns that will check multiple lines.

Given your example, it appears that you want to delete all lines that don't match #starthere or #endhere. Yes?
Think of the problem that way.

And, again, please use code tags when posting code or output. See the link in my signature.
 
Old 11-19-2019, 08:45 AM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Code:
list="
#starthere
dwfwef
efewf
wefewf
#endhere
"

sed -n '/^#/p' <<< "$list"
#starthere
#endhere
 
Old 11-19-2019, 08:51 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
or you might want to print that two lines explicitly
 
Old 11-19-2019, 08:58 AM   #6
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Grail , you are right , but i dont take notes for some codes or sometimes i forget to mention in code what that instruction does , then i get lost .

teckk , nope .


i only want to delete the lines between those 2 patterns , everything before and after those patterns should not be touched .

example :

Quote:
something
addfdf
dsfrre
#starthere
trjhtr
qweorqro
#endhere
4677ret
124253
fgrtrjuruj
Desired output :
Quote:
something
addfdf
dsfrre
#starthere
#endhere
4677ret
124253
fgrtrjuruj
with :
Quote:
sed -n '/\#starthere/,/\#endhere/p' file
i am almost there , but if i change the p to d then will delete everything
 
Old 11-19-2019, 09:41 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Code:
sed '/\#starthere/,/\#endhere/{d}' file
You can group more than one command between {}. They will only be executed when you are between the matched region. Use this to match the lines you want to preserve. There have already been good suggestions on how to preserve them. You can match each one explicitly or both with a RegEx, you can preserve them by explicitly printing or by jumping to the end via 'b' command.

Last edited by crts; 11-19-2019 at 09:43 AM. Reason: Formatting, Typo
 
Old 11-19-2019, 09:43 AM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
apple
banana
cherry
starthere
dwfwef
efewf
wefewf
endhere
peach
pear
pineapple
... this sed ...
Code:
sed '/^starthere/,/^endhere/{//!d;};' $InFile >$OutFile
... produced this OutFile ...
Code:
apple
banana
cherry
starthere
endhere
peach
pear
pineapple
I didn't write this, credit to author SLePort. I found it at:
https://stackoverflow.com/questions/...ching-patterns

Daniel B. Martin

.
 
3 members found this post helpful.
Old 11-19-2019, 09:54 AM   #9
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Thank Daniel , it works perfectly
 
  


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
[SOLVED] sed print text between two patterns inclusive, unless a third pattern is present Turbocapitalist Programming 11 09-21-2017 03:22 PM
[SOLVED] Search multiple patterns & print matching patterns instead of whole line Trd300 Linux - Newbie 29 03-05-2012 07:41 PM
How do I replace the text between patterns located on separate lines? (sed, awk, etc) Quon Programming 5 02-12-2012 06:27 AM
Sed. Delete blank lines between two patterns supersoni3 Programming 5 07-29-2010 10:40 AM
Remembering patterns and printing only those patterns using sed bernie82 Programming 5 05-26-2005 05:18 PM

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

All times are GMT -5. The time now is 03:22 PM.

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