LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 04-28-2013, 02:56 AM   #1
castor0troy
LQ Newbie
 
Registered: Apr 2013
Posts: 27

Rep: Reputation: Disabled
using sed to replace multiple strings


Hi Guys
I know this has been covered in the past but there is a small twist/

a.txt is
redcat
redhat
bluebat

b.txt is
cat mat
hat tat
.
.thousand keywords

i want to use sed to replace cat with mat[example] from b.txt

output should be
redmat
redtat
bluebat

iv tried the regular sed command but how do i do it for thousand of keywords?

thanks
 
Old 04-28-2013, 03:06 AM   #2
jdkaye
Senior Member
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 4,763

Rep: Reputation: Disabled
Quote:
Originally Posted by castor0troy View Post
iv tried the regular sed command but how do i do it for thousand of keywords?

thanks
Hi Castor,
Can you show us what you've go so far. It will make it easier to help you.
Ciao,
jdk
 
Old 04-28-2013, 03:09 AM   #3
castor0troy
LQ Newbie
 
Registered: Apr 2013
Posts: 27

Original Poster
Rep: Reputation: Disabled
Hi
ive got the results using sed.
$sed 's/string1/string2/' file1 > file1


the questions is how do i incorprate file names in sed.
string1 and string 2 are in b.txt
example
string1=cat
string2=mat
 
Old 04-28-2013, 03:10 AM   #4
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 3,459

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
Use sed to make a sed interpreter script from your b.txt. http://www.grymoire.com/Unix/Sed.html#uh-20
 
1 members found this post helpful.
Old 04-28-2013, 04:46 AM   #5
castor0troy
LQ Newbie
 
Registered: Apr 2013
Posts: 27

Original Poster
Rep: Reputation: Disabled
thanks.
this link helped.
 
Old 04-28-2013, 05:16 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
Even you can replace the word cat with mat in vi editor, as:
Code:
~$ vi b.txt
:%s/cat/mat/g
 
Old 04-28-2013, 12:36 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
allend's suggestion of creating a sed interpreter script is probably the definitive solution. Another possibility is to loop over all line entries in b.txt:
Code:
while read from to; do
   echo sed -i s/$from/$to/g a.txt
done < b.txt
This only works if the substitution text contains no whitespace. It is left as an exercise to the reader to make the script actually work on real data files. Test on backup data first.
--- rod.
 
Old 04-29-2013, 11:42 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
A trick I would probably use would be to run one sed command inside a process substitution, to reformat the file into a sed script that can be used directly in a second instance.

Code:
sed -f <( sed -r 's|(\w+) (\w+)|s/\1/\2/|' b.txt )  a.txt
 
  


Reply

Tags
sed


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
[SOLVED] sed help. Search and replace multiple strings with one command. dbrazeau Programming 4 02-13-2013 11:45 AM
[SOLVED] how we can find and replace multiple file using sed command jayakumar01 Linux - Server 1 04-05-2012 07:53 AM
Sed / Replace multiline, multiple instances jkmaster Programming 8 01-28-2010 09:00 AM
Search and Replace with multiple-line strings ChristianNerds.com Programming 4 08-21-2005 02:32 PM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM


All times are GMT -5. The time now is 06:30 AM.

Main Menu
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