LinuxQuestions.org
Visit Jeremy's Blog.
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, 01: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 01:11 PM.
 
Old 07-29-2010, 01:42 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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, 02: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, 02:08 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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, 02:12 PM   #5
b0uncer
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 02:15 PM. Reason: A small explanation won't hurt
 
Old 07-29-2010, 02:18 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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, 02:27 PM   #7
b0uncer
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 02:29 PM.
 
1 members found this post helpful.
Old 07-29-2010, 02: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 02:39 PM.
 
Old 07-29-2010, 02:38 PM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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, 02: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, 02:58 PM   #11
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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, 03: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, 03:03 PM   #13
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Could you paste one full line from your string1.txt file please?
 
Old 07-29-2010, 03: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, 03:11 PM   #15
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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 03:18 PM. Reason: added where I set $variable = $( ... )
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 10:09 AM
Python: replacing strings in a file tonya11en Programming 2 05-19-2009 07:52 PM
searching contents of .class file within jar (for strings) wakeboarder3780 Linux - Newbie 4 02-17-2009 02:49 PM
replacing strings in many files using tcsh mcbenus Linux - Software 5 03-03-2008 06:50 PM
Searching files for strings tmoorman Linux - Software 4 01-08-2004 02:46 PM


All times are GMT -5. The time now is 10:21 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration