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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.
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.