LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 07-29-2010, 12:05 PM   #1
xndd
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Rep: Reputation: 0
Question 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

Last edited by xndd; 07-29-2010 at 12:11 PM.
 
Old 07-29-2010, 12:42 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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.
 
Old 07-29-2010, 01:03 PM   #3
xndd
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Thanx for your reply but when i type your codes, it says:

Code:
/bin/sh: line x: read: `string1.txt': not a valid identifier
 
Old 07-29-2010, 01:08 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by xndd View Post
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.
 
1 members found this post helpful.
Old 07-29-2010, 01:12 PM   #5
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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
 
Old 07-29-2010, 01:18 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by b0uncer View Post
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.
 
Old 07-29-2010, 01:27 PM   #7
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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...

Last edited by b0uncer; 07-29-2010 at 01:29 PM.
 
1 members found this post helpful.
Old 07-29-2010, 01:32 PM   #8
xndd
LQ Newbie
 
Registered: Jul 2010
Posts: 13

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

Last edited by xndd; 07-29-2010 at 01:39 PM.
 
Old 07-29-2010, 01:38 PM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by xndd View Post
@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 View Post
And i have some problems with " and / chacarters.
What problems? Where are those characters? Are they part of your filenames?
 
1 members found this post helpful.
Old 07-29-2010, 01:52 PM   #10
xndd
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
Old 07-29-2010, 01:58 PM   #11
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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.
 
Old 07-29-2010, 02:01 PM   #12
xndd
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Actually these characters and the expression are in my string1.txt file
 
Old 07-29-2010, 02:03 PM   #13
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Could you paste one full line from your string1.txt file please?
 
Old 07-29-2010, 02:06 PM   #14
xndd
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Original Poster
Rep: Reputation: 0
ok, i did it actually, but here again:
Code:
xxxx
<a href="http://blabla.com" title="blabla">blabla</a>
.
.
.
 
Old 07-29-2010, 02:11 PM   #15
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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:

Last edited by GrapefruiTgirl; 07-29-2010 at 02:18 PM. Reason: added where I set $variable = $( ... )
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching .txt file for (specific) strings and printing them to new file Hb_Kai Linux - General 7 02-18-2010 09:09 AM
Python: replacing strings in a file tonya11en Programming 2 05-19-2009 06:52 PM
searching contents of .class file within jar (for strings) wakeboarder3780 Linux - Newbie 4 02-17-2009 01:49 PM
replacing strings in many files using tcsh mcbenus Linux - Software 5 03-03-2008 05:50 PM
Searching files for strings tmoorman Linux - Software 4 01-08-2004 01:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration