The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find and replace text bobo UNIX for Dummies Questions & Answers 1 01-12-2006 10:17 AM
find and replace text with a variable? doublejz Shell Programming and Scripting 3 08-31-2005 07:37 PM
Replace Text deep.singh UNIX for Dummies Questions & Answers 2 05-31-2005 09:03 AM
Need to replace text in an awk cdunavent Shell Programming and Scripting 2 05-10-2005 08:01 AM
Replace Text with sed networkfre@k Shell Programming and Scripting 5 11-24-2004 02:52 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-12-2006
Registered User
 

Join Date: Jan 2006
Posts: 145
Find and replace text PLEASE HELP

Dear friends please help,

I have a large data file with few hundred lines. A small example is shown below:

datafile is a file with few hundred lines with the third column has many different character stings:

test 100 abc
test 134 bcd
test 356 cdf
test 831 dfg
test 720 fgh

I need to find these character abc and replace it with John, bcd and replace it with Mike, cdf and replace it with Tom, dfg and replace it with Kelvin, and fgh and replace it with Andy

Once I finish the find and repacement my file should be:

test 100 John
test 134 Mike
test 356 Tom
test 831 Kelvin
test 720 Andy

I started with something this but it did not work:

sed 's/abc/John/g' datafile > temp
rm datafile
sed 's/bcd/Mike/g' temp > datafile
rm temp
sed 's/cdf/Tom/g' datafile > temp
rm datafile
sed 's/dfg/Kelvin/g' temp > datafile
rm temp
sed '/fgh/Andy/g' datfile > temp

Please help...I am very new with Unix..I am self learning the language for 2 weeks now!
Reply With Quote
Forum Sponsor
  #2  
Old 01-12-2006
linuxpenguin's Avatar
Registered User
 

Join Date: May 2002
Location: India
Posts: 295
what do you mean by it didnt work, was there no output, or was the output not as expected was there error in your commands. to figure out the solution we need to first figure out what went wrong, then we can find where to fix it, isnt it.
what i think is the above should work. and to do a multiple search and replace you can use the -e flag with sed. so you can do

sed -e 's/abc/John/g' -e 's/bcd/Mike/g' -e 's/cdf/Tom/g' ...... datafile > temp

dont take those dots literally, they represent you can add more -e commands
__________________
War doesnt determine who is right, it determines who is left
Reply With Quote
  #3  
Old 01-12-2006
Registered User
 

Join Date: Jan 2006
Posts: 145
It works! Thank you very much!

Now I have other problems:

in Microsoft Excel the have these data:

-e 's/abc/john/g'
-e 's/dcf/tom/g'
-e 's/hgh/mike/g'

and many more lines.

How can I combine these lines in the form that Unix can read? Which is :

-e 's/abc/john/g' -e 's/dcf/tom/g' -e 's/hgh/mike/g'.......datafile > temp

Please advise

I copy these lines in excel paste it in notepad and it give line per line.
Reply With Quote
  #4  
Old 01-12-2006
Registered User
 

Join Date: Dec 2005
Posts: 49
bobo, copy all the lines down within excel and go to the Edit->Paste especial and use "transpose".
Reply With Quote
  #5  
Old 01-12-2006
Registered User
 

Join Date: Jan 2006
Posts: 145
Thank you for your reply! Transpose do not work! I use concatenate(a1,a2,....only up to 30) I have 500 lines!
Reply With Quote
  #6  
Old 01-12-2006
Registered User
 

Join Date: Dec 2005
Posts: 49
It worked for me. See the images attachments.
Attached Images
File Type: jpg excel1.JPG (8.0 KB, 18 views)
File Type: jpg excel2.JPG (10.5 KB, 10 views)
File Type: jpg excel3.JPG (14.5 KB, 10 views)
File Type: jpg excel4.JPG (20.3 KB, 9 views)
File Type: jpg excel5.JPG (20.3 KB, 8 views)
Reply With Quote
  #7  
Old 01-12-2006
Registered User
 

Join Date: Jan 2006
Posts: 145
Thank you very much!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0