LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-15-2010, 08:25 AM   #1
CGP314
LQ Newbie
 
Registered: Jun 2010
Posts: 5

Rep: Reputation: 0
sed -- replacing a string in a file with the contents of another file?


Hello,

I would like to know how I can replace a string in one file with the complete contents of another life.

I assume that sed is the way to go, but I can't quite figure it out.

Thanks for any help.
 
Old 06-15-2010, 09:44 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
^ do you have any examples of what you are trying to do ?
a human life mite be rather difficult; a cat mite be more plausible since they got so many.
more information is needed.
 
Old 06-15-2010, 09:45 AM   #3
CGP314
LQ Newbie
 
Registered: Jun 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Here's an example,

I have an index.html file that contains the string <!--NAVIGATIONBAR-->. I want to replace that string with the contents of a file called navigationbar.html.
 
Old 06-15-2010, 10:41 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
this is a little quick-and-dirty,
but maybe you can use grep -n NAVIGATIONBAR to find the line you want to substitute
then sed -n 1,`expr $navpos - 1` index.html > index-2.html
then do a cat navigationbar.html >> index-2.html
then do sed -n `expr $navpos - 1`,999999 index.html >> index-2.html

sorry i am at work so i cant think of the sed 1-liner; this is also untested so have fun playing around with it.
 
Old 06-15-2010, 11:08 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
If the string to replace is on its own line, you can try to read (and insert) the content of the file using the r command, then delete the line:
Code:
sed '/<!--NAVIGATIONBAR-->/ {
  r navigationbar.html
  d
}' index.html
This is documented on Grymoire's sed tutorial: http://www.grymoire.com/Unix/Sed.html#uh-37
 
1 members found this post helpful.
Old 06-17-2010, 07:14 AM   #6
CGP314
LQ Newbie
 
Registered: Jun 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
sed '/<!--NAVIGATIONBAR-->/ {
r navigationbar.html
d
}' index.html

That works great. Now the only problem is when I move the files into separate directories, it no longer works. Do I need to specify the path in a special way inside sed?

thanks so much.


Edited to add:

It looks like it will work with an absolute path, but not a relative path. Any ideas?

Last edited by CGP314; 06-17-2010 at 09:58 AM.
 
Old 06-17-2010, 09:52 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by CGP314 View Post
That works great. Now the only problem is when I move the files into separate directories, it no longer works. Do I need to specify the path in a special way inside sed?
Nope. You can specify it as relative or absolute path. The only caveat is that sed doesn't know about shell's environment variables. Therefore you cannot use $HOME for example, unless you let the shell expands it by using double quotes to embed the sed command (if possible).
 
1 members found this post helpful.
Old 06-17-2010, 10:23 AM   #8
CGP314
LQ Newbie
 
Registered: Jun 2010
Posts: 5

Original Poster
Rep: Reputation: 0
OK, got it all figured out. There were spaces in my path name that require the file name being read to be enclosed in single quotes.

Thanks for your help everyone.
 
Old 06-17-2010, 04:13 PM   #9
CGP314
LQ Newbie
 
Registered: Jun 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Code:
sed '/<!--NAVIGATIONBAR-->/ {
r navigationbar.html
d
}' index.html
Ah, one more problem... I'm trying to overwrite the index file like so:

Code:
sed '/<!--NAVIGATIONBAR-->/ {
r navigationbar.html
d
}' index.html | cat > index.html
But I end up with a zero byte file. Is there another way to overwrite index.html?
 
Old 06-17-2010, 04:22 PM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by CGP314 View Post
Code:
sed '/<!--NAVIGATIONBAR-->/ {
r navigationbar.html
d
}' index.html
Ah, one more problem... I'm trying to overwrite the index file like so:

Code:
sed '/<!--NAVIGATIONBAR-->/ {
r navigationbar.html
d
}' index.html | cat > index.html
But I end up with a zero byte file. Is there another way to overwrite index.html?
Hi,

use sed's -i option, like
Code:
sed -i '/<!--NAVIGATIONBAR-->/ {
r navigationbar.html
d
}' index.html
 
Old 06-17-2010, 04:29 PM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Yes. Take a look at the -i option of sed. It will edit the file in place. You can also let it automatically do a backup for safety. Suppose you want to have a backup named index.html.bck: the option will be -i.bck.

A note about your command: piping the output to cat and then redirect it is not necessary, since you can directly redirect the output to a file. Anyway, redirecting output to the input file itself is an error, because the first thing performed by the shell is to create an empty output file then execute the command.

This is the reason why it's very common to redirect standard output to a temporary file, then rename it as the original file. As far as I know, sed is the only command line editor that offers the possibility to edit the original file through the option -i. I never dived into the sed's source code, but I suspect it actually does the same: it writes the output to a temporary location, then renames it for you, overwriting the original file.
 
1 members found this post helpful.
  


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
Replacing character position within the string of a file scroy Linux - Newbie 13 11-08-2008 05:45 PM
sed: print section of file from string to end of file samyboy Linux - Newbie 4 02-26-2008 07:23 AM
replacing string within same file tostay2003 Programming 2 07-06-2007 02:25 AM
[SOLVED] Replacing using sed with variables from another file? eamesj Programming 5 11-24-2006 10:20 AM
Replacing String with File Content in Sed meshcurrent Linux - General 2 06-01-2003 12:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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