|
|
|
Need help with a search and replace logi
|
Original Message
|
Name: Jaggar
Date: June 5, 2002 at 13:58:30 Pacific
Subject: Need help with a search and replace logi
|
Comment: I need some help on a unix script I am trying to write, or at least the logic behind it. The task im trying to achieve is to search a file for using a key word, once it finds this word, it should replace the entire line with another string. For example, I have the following data in a file called myfile. Name: John Doe Street: 102 Chestnut St City: New York Zip: 13892 Now I want to do a search for the key word like City and once I find it, I need it to replace that entire line with “City: Philadelphia”. So my when I vi the file again, I will see: Name: John Doe Street: 102 Chestnut St City: Philadelphia Zip: 13892 This seemed harder then I imagined, I need this to be in a script so I can modify many files at once like this. Im not sure how to, should I use sed? grep? Can you give me a helping hand.
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: Panic
Date: June 5, 2002 at 14:36:31 Pacific
Subject: Need help with a search and replace logi
|
Reply: (edit)Use the vi editor it´s ugly but have many funcions, type man vi for complete(many pages) reference
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: Panic
Date: June 5, 2002 at 14:40:49 Pacific
Subject: Need help with a search and replace logi
|
Reply: (edit)PD i have a pdf book for shell programming it´s good if you want it tell me
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: jaggar
Date: June 5, 2002 at 16:22:27 Pacific
Subject: Need help with a search and replace logi
|
Reply: (edit)I think you missed the point, there are too many files to go and vi and edit, i need to write a script to do this.
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: LANkrypt0
Date: June 6, 2002 at 05:16:55 Pacific
Subject: Need help with a search and replace logi |
Reply: (edit)Sed should work fine. I did: sed -e "s/City: New York/City: Philadelphia/g" test2 And that worked just fine.
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: LANkrypt0
Date: June 6, 2002 at 06:54:51 Pacific
Subject: Need help with a search and replace logi |
Reply: (edit)Just wrote a little script that will perform changes to any file in the directory (so move[mv] or copy[cp] the files you need to change to a empty directory[mkdir], edit the script and run it. #!/bin/ksh list=`ls` for var in $list;do sed -e "s/City: Philadelphia/City: New York/g" -e "s/City: Dallas/City: Los Angeles/g" -e "s/City: Denver/City: Colorado/g" $var > $var.temp mv $var.temp $var done That changes this: Name: John Doe Street: 102 Chestnut St City: Philadelphia Zip: 13892 Name: John Doe Street: 102 Chestnut St City: Denver Zip: 13892 Name: John Doe Street: 102 Chestnut St City: Dallas Zip: 13892 to this: Name: John Doe Street: 102 Chestnut St City: New York Zip: 13892 Name: John Doe Street: 102 Chestnut St City: Colorado Zip: 13892 Name: John Doe Street: 102 Chestnut St City: Los Angeles Zip: 13892 btw those sed -e commands should be on the same line: sed -e "s/City: Phi....ado/g" $var > $var.temp
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: LANkrypt0
Date: June 6, 2002 at 07:16:08 Pacific
Subject: Need help with a search and replace logi |
Reply: (edit)Sorry wasnt thinking this morning: #!/bin/ksh list=`ls` for var in $list;do sed \ -e "s/City: Philadelphia/City: New York/g" \ -e "s/City: Dallas/City: Los Angeles/g" \ -e "s/City: Denver/City: Colorado/g" $var > $var.temp mv $var.temp $var done If you need to add more searches, just follow the same format and do: -e "s/search/replace/g" \ The "\" needs to be at the and and the last line gets no "\" but gets the $var > $var.temp
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: Jaggar
Date: June 6, 2002 at 08:56:14 Pacific
Subject: Need help with a search and replace logi
|
Reply: (edit)Lankrypt, Thanks for that advice, one of the main problem im having is that I do not have a list fo all the cities and i want it to replace it dynamicly. So it could be City:Hickstown and it should pick that line and convert it to City:Philadelphia. Let me know if you have any idea to do this.
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: LANkrypt0
Date: June 6, 2002 at 10:30:45 Pacific
Subject: Need help with a search and replace logi |
Reply: (edit)No problem, let me know if this works. #!/bin/ksh stty erase ^? list=`ls` function changecities { read cityfind'?Enter the City to Find> ' read cityreplace'?Enter City to Replace With> ' for var in $list;do sed -e "s/City: $cityfind/City: $cityreplace/g" $var > $var.temp mv $var.temp $var done echo "" read yna'?Change More Cities? (y/n) ' echo "" case $yna in y*|Y*) changecities ;; n*|N*) exit ;; *) exit ;; esac } changecities That will give you the option to enter in the city to look for, then the city to change it to, then the option to keep making changes.
Report Offensive Follow Up For Removal
|
|
Response Number 9
|
Name: Jaggar
Date: June 6, 2002 at 12:44:25 Pacific
Subject: Need help with a search and replace logi
|
Reply: (edit)Anyway to do this without always asking for the city to find, there will be too many files to process at once. I will need City:XXXXXX changed always to City:Philadelphia, where XXXXXX can be any city. Also another question non related: if i have --> :%s/find/replace/g and my find string is "/dir/cat" how can i make sure the / is not used as a unix keyword
Report Offensive Follow Up For Removal
|
|
Response Number 10
|
Name: LANkrypt0
Date: June 6, 2002 at 13:03:34 Pacific
Subject: Need help with a search and replace logi |
Reply: (edit)Ok this one :) looks for any instance of City and then prompts you for which city to change it to. #!/bin/ksh stty erase ^? list=`ls` function changecities { read cityreplace'?Enter City to Replace With> ' for var in $list;do sed -e "s/City: Philadelphia mv $var.temp $var done } changecities Same rule applies though, the files you want to change need to be in their OWN directory. And the separate problem all you need to do is add \ in front of the other slashes so it would read: s/\/find\/string/\/replace\/string/g ex: s/\/dir\/cat/\/bad\/cat/g
Report Offensive Follow Up For Removal
|
|
Response Number 11
|
Name: LANkrypt0
Date: June 6, 2002 at 13:20:01 Pacific
Subject: Need help with a search and replace logi |
Reply: (edit)My bad, put the wrong script in there, it should be: #!/bin/ksh stty erase ^? rtdir="/remote/directory" list=`cd $rtdir; ls` function changecities { read cityreplace'?Enter City to Replace With> ' for var in $list;do sed -e "s/City: .*/City: $cityreplace/g" $rtdir/$var > $rtdir/$var.temp mv $rtdir/$var.temp $rtdir/$var done } changecities I also added in the rtdir variable where you put in the full path to the directory where these files are located because if you have this script in the same directory it will also check the script for City: (which it will find) and replace it. Just be sure to specify the directory where it says /remote/directory
Report Offensive Follow Up For Removal
|
|
Response Number 12
|
Name: LANkrypt0
Date: June 6, 2002 at 13:23:46 Pacific
Subject: Need help with a search and replace logi |
Reply: (edit)*sigh* I these posting spaces need to be bigger, it chops the damned scripts. on the sed line, be sure to bring $var.temp up to the sed line to read: sed -e "s/City: .../.../g $rtdir/$var > $rtdir/$var.temp
Report Offensive Follow Up For Removal
|
|
Response Number 13
|
Name: Jaggar
Date: June 6, 2002 at 18:57:22 Pacific
Subject: Need help with a search and replace logi
|
Reply: (edit)Thanks so much, I will try this... ur very helpfull!!!
Report Offensive Follow Up For Removal
|
Use following form to reply to current message:
|
|

|