![]() |
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/" {} \;Code:
find /path -type f -exec sed -i "s/"string1.txt"/string2/" {} \; |
Hi,
how about reading the string into a variable first? Code:
read string1 string1.txt |
Thanx for your reply but when i type your codes, it says:
Code:
/bin/sh: line x: read: `string1.txt': not a valid identifier |
Quote:
Code:
read string1 < string1.txt |
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" '{}' \; |
Quote:
|
True, I assumed the "first and only line" policy. If there were several lines ("keywords"), a loop like
Code:
while read string1 |
@crts your code working but it seems that just reading first line? And i have some problems with " and / characters.
|
Quote:
Quote:
|
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>Code:
sed: couldn't open file blabla.com" title="blabla">blabla</a>/ /g: No such file or directory |
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/' |
Actually these characters and the expression are in my string1.txt file
|
Could you paste one full line from your string1.txt file please?
|
ok, i did it actually, but here again:
Code:
xxxx |
Code:
sasha@reactor: cat string1.txtEDIT: 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 |
| All times are GMT -5. The time now is 05:31 AM. |