LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Searching and replacing strings in a file with strings in other files (http://www.linuxquestions.org/questions/linux-newbie-8/searching-and-replacing-strings-in-a-file-with-strings-in-other-files-822893/)

xndd 07-29-2010 12:05 PM

Searching and replacing strings in a file with strings in other files
 
Hi all,
I want to search and replace strings in a file with strings in other files

I used this code for replacing and it is working well:

Code:

find /path -type f -exec sed -i "s/string1/string2/" {} \;
but i need to do it with big strings(string1 is big) and i want to use a txt file for this.But this code not working :

Code:

find /path -type f -exec sed -i "s/"string1.txt"/string2/" {} \;
pls help me

crts 07-29-2010 12:42 PM

Hi,

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.

xndd 07-29-2010 01:03 PM

Thanx for your reply but when i type your codes, it says:

Code:

/bin/sh: line x: read: `string1.txt': not a valid identifier

crts 07-29-2010 01:08 PM

Quote:

Originally Posted by xndd (Post 4049346)
Thanx for your reply but when i type your codes, it says:

Code:

/bin/sh: line x: read: `string1.txt': not a valid identifier

Oops, it should have been:
Code:

read string1 < string1.txt
Sorry about that.

b0uncer 07-29-2010 01:12 PM

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.

crts 07-29-2010 01:18 PM

Quote:

Originally Posted by b0uncer (Post 4049354)
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.

b0uncer 07-29-2010 01:27 PM

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...

xndd 07-29-2010 01:32 PM

@crts your code working but it seems that just reading first line? And i have some problems with " and / characters.

crts 07-29-2010 01:38 PM

Quote:

Originally Posted by xndd (Post 4049376)
@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 (Post 4049376)
And i have some problems with " and / chacarters.

What problems? Where are those characters? Are they part of your filenames?

xndd 07-29-2010 01:52 PM

I tried ${string1} in b0uncer's method and working but i have still " , / character problems. In the line there is this expression:
Code:

<a href="http://blabla.com" title="blabla">blabla</a>
while running code, it says that error:
Code:

sed: couldn't open file blabla.com" title="blabla">blabla</a>/ /g: No such file or directory

GrapefruiTgirl 07-29-2010 01:58 PM

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.

xndd 07-29-2010 02:01 PM

Actually these characters and the expression are in my string1.txt file

GrapefruiTgirl 07-29-2010 02:03 PM

Could you paste one full line from your string1.txt file please?

xndd 07-29-2010 02:06 PM

ok, i did it actually, but here again:
Code:

xxxx
<a href="http://blabla.com" title="blabla">blabla</a>
.
.
.


GrapefruiTgirl 07-29-2010 02:11 PM

Code:

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:



All times are GMT -5. The time now is 04:22 PM.