![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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! |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
bobo, copy all the lines down within excel and go to the Edit->Paste especial and use "transpose".
|
|
#5
|
|||
|
|||
|
Thank you for your reply! Transpose do not work! I use concatenate(a1,a2,....only up to 30) I have 500 lines!
|
|
#6
|
|||
|
|||
|
It worked for me. See the images attachments.
|
|
#7
|
|||
|
|||
|
Thank you very much!
|
|||
| Google The UNIX and Linux Forums |