ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
That's an interesting idea, and it would probably work except for your choice of quotes. Use double quotes instead of single quotes:
Code:
cat index.html.old | sed "s@`cat find.txt`@`cat replace.txt`@" > index.html
The shell treats anything inside single quotes as literal text, whereas the command actually wants the shell to do command substitution inside the sed expression.
EDIT:
Just an example of what I'm getting at:
Code:
$ echo "$DISPLAY"
:0.0
$ echo '$DISPLAY'
$DISPLAY
Last edited by Dark_Helmet; 10-05-2005 at 06:58 PM.
This is a test
This is another test
Another line is a test
The fourth line is a test
Lastly, the fifth line contains a test
./find.txt
Code:
is a test
./replace.txt
Code:
replaced
Command:
Code:
$ cat input_file | sed "s@`cat find.txt`@`cat replace.txt`@" > processed_output
$ cat processed_output
This replaced
This is another test
Another line replaced
The fourth line replaced
Lastly, the fifth line contains a test
You will run into problems if your find or replacement text contains special characters. For instance, since the 's' command to sed uses that '@' symbol to denote the pattern match, the replacement, and end of the sed command, the find.txt and replace.txt files can't contain '@' unless they are escaped: \@
Similarly, neither find.txt or replace.txt can have a double-quote ( " ) in them, because that will end the sed command (it matches/closes the double-quote right before s@). You can use them, but again, they need to be escaped: \"
You also need to be aware that sed will try to interpret find.txt's contents as a regular expression. That might have undesired results.
Yeah, I figured that out. I'm trying to replace some actual HTML, which I think was throwing it off. I came up with this:
Code:
cat index.html.old | sed -e '/<!-- START -->/,/<!-- END -->/c\`cat replace.txt`'
The problem with this is that it literally prints `cat replace.txt` where the new HTML should go. Is there a way I can get it to put the results of the cat command there?
The shell will do no interpretation at all to text within a pair of single quotes. No variable expansion, no command substitution, no file globbing, nada.
Yeah, the exclamation mark is special to bash. You'll need to escape it too.
In case you're curious, it's the history operator (or something like that). It executes the last command that starts with the text immediately after it. So, in this case, it was looking for a command in the shell history that started with two dashes.
You can't have several lines of text in the regular expression to be searched without adding lines to the line buffer, and you might need to use the Hold buffer also.
You should use a form such as:
sed -f replacehtml.sed index.html.old >index.html
where replacehtml.sed is a file of sed commands.
There may be several cases to consider, such as when the pattern to be replaced is split between several lines, if two patterns to be replaced might occur on the same line, if you have a pattern on one line and the beginning of another pattern on the same line.
After you have developed your sed program, test it out on several files before process all of the files.
You can process them in a bash script like:
for htmlfile in *.html; do
sed -f changehtml.sed $htmlfile ${htmlfile%.html}.new.html
done
If what you are doing is simply replacing html files with references to those files, then a script that takes the names and enters the names into a new document may be a better option. If you use bash, consider using "printf". It is more flexible than using "echo".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.