LinuxQuestions.org
Review your favorite Linux distribution.
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 03-12-2024, 03:17 PM   #1
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Rep: Reputation: 78
sed help


I kinda suck with sed.

I need sed (gnu or other) expression to remove lines inclusive of a from pattern to an end pattern.
"marker1" and "marker2" are the text patterns.


from:
Code:
<tag1>
<!-- marker1 -->
d
d
d
<!-- marker2 -->
</tag1>
to:
Code:
<tag1>
</tag1>
 
Old 03-12-2024, 03:41 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Linux_Kidd View Post
I kinda suck with sed. I need sed (gnu or other) expression to remove lines inclusive of a from pattern to an end pattern. "marker1" and "marker2" are the text patterns. from:
Code:
<tag1>
<!-- marker1 -->
d
d
d
<!-- marker2 -->
</tag1>
to:
Code:
<tag1>
</tag1>
Sounds like your other thread:
https://www.linuxquestions.org/quest...pt-4175734593/

There are a lot of ways to do this in sed; putting "sed remove from between patterns" into any search-engine will give you lots. You say you're stuck, but don't show us what you're stuck ON, or show your efforts. This also seems like some sort of JSON/XML/HTML code, and there are FAR better tools to parse up HTML/JSON/structured data.
 
Old 03-12-2024, 03:41 PM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Code:
keithhedger@Marvin [ ~ ]$ echo '<tag1>
<!-- marker1 -->
d
d
d
<!-- marker2 -->
</tag1>'|sed '/<!-- marker1 -->/,/<!-- marker2 -->/d'
<tag1>
</tag1>
TB0ne types faster than me

Last edited by Keith Hedger; 03-12-2024 at 03:42 PM.
 
1 members found this post helpful.
Old 03-12-2024, 09:03 PM   #4
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by TB0ne View Post
Sounds like your other thread:
https://www.linuxquestions.org/quest...pt-4175734593/

There are a lot of ways to do this in sed; putting "sed remove from between patterns" into any search-engine will give you lots. You say you're stuck, but don't show us what you're stuck ON, or show your efforts. This also seems like some sort of JSON/XML/HTML code, and there are FAR better tools to parse up HTML/JSON/structured data.
Between patterns I had working after modifications.
But then my other code didn't work ok with that.
So now I needed to do pattern to pattern inclusive delete.
Like I said, my sed skills are crappy, and I am testing sed between ubuntu and android, and the android sed comes from in toybox, I have to escape the "!" otherwise sed fails.

But I don't really need to pattern match the whole marker tag, just need to pattern the words "marker1" and "marker2", which means no more "!" issue.

A greatful thanks for the replies.

Last edited by Linux_Kidd; 03-12-2024 at 09:04 PM.
 
1 members found this post helpful.
Old 03-13-2024, 02:02 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
it is answered, I think. I can add only a remark: you can use . (dot) instead of those strange characters (like |), sometimes that helps.
We have no info about your other code, therefore can't help on that.
 
Old 03-13-2024, 02:53 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Perhaps:
Code:
ptndel () {
    local File="$1"
    local SedCmd='s;[].*[\\/];\\&;g'
    local From=$(sed "$SedCmd" <<<"$2")
    local To=$(sed "$SedCmd" <<<"$3")
    sed "/$From/,/$To/d" "$File"
}
test:
Code:
ptndel - ".*" "[]" <<"DONE"
elso
.*
kivag
[]
utolso
DONE
result:
Code:
elso
utolso

Last edited by NevemTeve; 03-13-2024 at 03:41 AM. Reason: Bugfix
 
Old 03-13-2024, 03:32 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Quote:
Originally Posted by NevemTeve View Post
Perhaps:
Code:
ptndel () {
    local File="$1"
    local SedCmd='s;[\[\]\.\\\/];\\&;g'
    local From=$(sed "$SedCmd" <<<"$2")
    local To=$(sed "$SedCmd" <<<"$3")
    sed "/$From/,/$To/d "$File"
}
you can escape chars in shell too: https://stackoverflow.com/questions/...hen-using-bash
 
  


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 help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

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

All times are GMT -5. The time now is 11:20 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