Find and replace text PLEASE HELP


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find and replace text PLEASE HELP
# 1  
Old 01-12-2006
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!
# 2  
Old 01-12-2006
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
# 3  
Old 01-12-2006
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  
Old 01-12-2006
bobo, copy all the lines down within excel and go to the Edit->Paste especial and use "transpose".
# 5  
Old 01-12-2006
Thank you for your reply! Transpose do not work! I use concatenate(a1,a2,....only up to 30) I have 500 lines!
# 6  
Old 01-12-2006
It worked for me. See the images attachments.
Find and replace text PLEASE HELP-excel1jpg
Find and replace text PLEASE HELP-excel2jpg
Find and replace text PLEASE HELP-excel3jpg
Find and replace text PLEASE HELP-excel4jpg
Find and replace text PLEASE HELP-excel5jpg

# 7  
Old 01-12-2006
Thank you very much!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a text and if condition matches then replace it

Need a script that can find text in a file and replace it accordingly. This is the file I have: while IFS=',' read -r f1 f2 f3 do { nohup /home/testuser/dbmaintenance/sys_offline_maintenance.sh $f1 $f2 $f3 > $f2.out & } done < "/home/testuser/dbmaintenance/week1offlineserver.txt" In... (4 Replies)
Discussion started by: singhhe
4 Replies

2. UNIX for Beginners Questions & Answers

Find and replace a string in a text file

Dear all, I want to find all the "," in my text file and then replace the commas to a tab. I found a script online but I don't know how to modify the script for my case. Any one can help? Thank you. @echo off &setlocal set "search=%1" set "replace=%2" set "textfile=Input.txt" set... (2 Replies)
Discussion started by: forevertl
2 Replies

3. Shell Programming and Scripting

How to find text and replace next line.?

Have multiple files with the same format and certain text is the same. Have specific text to search for and then need to replace the next line. Can search on the text DEVICE and multiple lines will be found. The line after each DEVICE line has {. Want to replace the line { with {-someadditiontext.... (2 Replies)
Discussion started by: bigdaddywags
2 Replies

4. UNIX for Advanced & Expert Users

Find and replace the line in text file

I have two files a.txt b.txt I want to find a line in a.txt and replace by another line from b.txt a.txt asfsdfsfsfdfsf asfwererfgdgf wrerwetretfdg b.txt werdfgdfgf werergfdgd sfdfgfgfgfgg i want to replace the 1st line of a.txt by 1st line of b.txt i want out put as (5 Replies)
Discussion started by: rammm
5 Replies

5. Shell Programming and Scripting

Find and add/replace text in text files

Hi. I would like to have experts help on below action. I have text files in which page nubmers exists in form like PAGE : 1 PAGE : 2 PAGE : 3 and so on there is other text too. I would like to know is it possible to check the last occurance of Page... (6 Replies)
Discussion started by: lodhi1978
6 Replies

6. Shell Programming and Scripting

find pattern and replace the text before it

i am editing a big log file with the following pattern: Date: xxxx Updated: name Some log file text here Date: eee Updated: ny Some log file text here Basically i want to remove all the text in a line before the "Updated" pattern. I sill want to print the other... (4 Replies)
Discussion started by: balan1983a
4 Replies

7. Shell Programming and Scripting

sed to find replace mutliline text

Hi, My input file form 1 fill 2 fill 3 form 4 fill 5 form 6 fill 7 form 8 Now i need to substiute according to the fill. form followed by single fill need to be replced with category 1 form with above and below fill need to be repalced with category 2 (5 Replies)
Discussion started by: vasanth.vadalur
5 Replies

8. Shell Programming and Scripting

find text but replace a text beside it

I have an html file that looks like this (this is just a part of the html file): <td colspan="3" rowspan="1" style="text-align: center; background-color: rgb(<!-- IDENTIFIER1 -->51, 255, 51);"><small><!-- IDENTIFIER2 -->UP</small></td> This is to automatically update the status of the... (4 Replies)
Discussion started by: The One
4 Replies

9. UNIX for Dummies Questions & Answers

Find and replace text

test 100 abc test 134 bcd test 356 cdf test 831 dfg test 720 fgh Please advise how can I replace the abc, bcd....with ABC, BCD.... (1 Reply)
Discussion started by: bobo
1 Replies

10. Shell Programming and Scripting

find and replace text with a variable?

Is there a way that I can make the following work with using variables? perl -pi -e 's#blah#hrm#ig' replacetext but like this var=blah perl -pi -e 's#$var#hrm#ig' replacetext (3 Replies)
Discussion started by: doublejz
3 Replies
Login or Register to Ask a Question

Featured Tech Videos