sed -- replacing a string in a file with the contents of another file?
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
^ 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.
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.
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.
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?
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).
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.