[SOLVED] Searching and replacing strings in a file with strings in other files
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.
how about reading the string into a variable first?
Code:
read string1 string1.txt
find /path -type f -exec sed -i "s/$string1/<your substitution text>/g" {} \;
This should work if string1 is on the first line in string1.txt and the only string to replace. If you have several strings to replace then you should consider using a loop.
No need for such complexity, this should work too:
Code:
find /path -type f -exec sed -i "s/$(cat 'string1.txt')/<your substitution text>/g" '{}' \;
The $(command) translates into English as "run 'command' and place the output here". I always quote the curly braces too ('{}' instead of just {}), to make sure fancy filenames don't cause problems.
Last edited by b0uncer; 07-29-2010 at 01:15 PM.
Reason: A small explanation won't hurt
No need for such complexity, this should work too:
Code:
find /path -type f -exec sed -i "s/$(cat 'string1.txt')/<your substitution text>/g" '{}' \;
The $(command) translates into English as "run 'command' and place the output here". I always quote the curly braces too ('{}' instead of just {}), to make sure fancy filenames don't cause problems.
It probably will as long as there is no '\n' at the end of the line. The OP needs to explain in more detail what exactly he is trying to achieve, e. g. is there only one string in one file, several strings in one/several files etc.
True, I assumed the "first and only line" policy. If there were several lines ("keywords"), a loop like
Code:
while read string1
do
find /path -type f -exec sed -i "s/$string1/<your substitution text>/g" '{}' \;
done < string1.txt
would be needed to read the lines one by one. I'd be tempted to try out Perl for that too, given that it's easy to chomp off the newline marks and other oddities from the data with it. But more can be done once the OP describes the problem further...
@crts your code working but it seems that just reading first line?
Yes, that is exactly what I said. What does your file look like? Are all strings that need to be replaced in one file? If yes, see b0uncer's solution but use ${string1} instead of just $string.
Quote:
Originally Posted by xndd
And i have some problems with " and / chacarters.
What problems? Where are those characters? Are they part of your filenames?
Problems with slashes are due to one side or the other of the sed expression, containing slashes, while at the same time, you are using slashes as your sed statement delimiters. Use a character that is NOT contained in the string variables, as a sed delimiter. Example:
Code:
sed 's/hello/goodbye/'
would be changed to:
sed 's/#hello#goodbye#'
So, in that example, I changed the sed delimiter to a # instead of a slash. Note that there may also be other special characters that need to be escaped, under certain circumstances.
sasha@reactor: cat string1.txt
<a href="http://blabla.com" title="blabla">blabla</a>
sasha@reactor: variable=$(cat string1.txt)
sasha@reactor: echo $variable
<a href="http://blabla.com" title="blabla">blabla</a>
sasha@reactor: sed -i "s|${variable}|Now it is gone|" string1.txt
sasha@reactor: cat string1.txt
Now it is gone
sasha@reactor:
OK, so I put your weird string into a file called string1.txt and used a pipe symbol ( | ) as my delimiter for sed. As you can see above, it worked. Does this explain how to fix it?
EDIT: And here is is the other way (putting the string back how it was):
Code:
sasha@reactor: sed -i "s|Now it is gone|${variable}|" string1.txt
sasha@reactor: cat string1.txt
<a href="http://blabla.com" title="blabla">blabla</a>
sasha@reactor:
Last edited by GrapefruiTgirl; 07-29-2010 at 02:18 PM.
Reason: added where I set $variable = $( ... )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.