LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-22-2012, 08:33 AM   #1
XXLRay
Member
 
Registered: May 2010
Posts: 133

Rep: Reputation: 16
Replace multi line pattern by text variable in sed


I have a shell script in which I want do replace a multi line pattern by the text inside a variable using sed (not awk or perl or any other stuff) such that
Code:
...
<!-- ontag -->
foo
bar
foobar
<!-- offtag -->
...
(Yes, it's in an html file)

becomes
Code:
...
<!-- ontag -->
new
multi
line
pattern
<!-- offtag -->
...
What I have so far is:
Code:
FILE="/my/file/name"
ANYVAR="<!-- ontag -->\nnew\nmulti\nline\npattern\n<!-- offtag -->"
sed ':a $!{N;ba}; s/<!-- ontag -->.*<!-- offtag -->/'"${ANYVAR}"'/g' -i $FILE
But this just produces:
Code:
...
${ANYVAR}
...
(It only prints the name of the variable instead of the content)

If I directly insert the pattern instead of a variable like:
Code:
sed ':a $!{N;ba}; s/<!-- ontag -->.*<!-- offtag -->/<!-- ontag -->\nmulti\nline\npattern\n<!-- offtag -->/g' -i $FILE
everything works fine so it seems as if it is just the way I access the variable.
 
Old 11-22-2012, 09:31 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
2 things to try:

1. Use weak quotes (") for the whole SED command string---and NO quotes on the expanded variable.

2. Keep the strong quotes on the whole string, and use only the weak quotes on the expanded variable.
 
Old 11-22-2012, 10:05 AM   #3
XXLRay
Member
 
Registered: May 2010
Posts: 133

Original Poster
Rep: Reputation: 16
1. offers ${NAVBAR} as replacement
Code:
sed ":a $!{N;ba}; s/<!-- ontag -->.*<!-- offtag -->/${ANYVAR}/g" -i $FILE
2. offers "${NAVBAR}" (with quotes) as replacemment
Code:
sed ':a $!{N;ba}; s/<!-- ontag -->.*<!-- offtag -->/"${ANYVAR}"/g' -i $FILE
Found the working one (at least there was no endless amount of combinations):
Code:
sed ':a $!{N;ba}; s/<!-- ontag -->.*<!-- offtag -->/'${ANYVAR}'/g' -i $FILE
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Recognize multi-line pattern and delete certain lines (awk/sed?) legato Programming 4 10-31-2012 04:02 PM
[SOLVED] How to escape single quotes in sed for multi line pattern XXLRay Linux - Software 1 07-18-2011 07:35 AM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
[SOLVED] Need sed help: s/ command won't replace two occurrences of pattern on same line GrapefruiTgirl Programming 7 12-16-2009 02:08 AM
replace a pattern with a line using sed/awk lokeshn05 Linux - Newbie 3 05-06-2009 03:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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