Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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: 13892Now 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: 13892This 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.

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.

Sed should work fine. I did:
sed -e "s/City: New York/City: Philadelphia/g" test2And that worked just fine.

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
doneThat changes this:
Name: John Doe
Street: 102 Chestnut St
City: Philadelphia
Zip: 13892Name: John Doe
Street: 102 Chestnut St
City: Denver
Zip: 13892Name: John Doe
Street: 102 Chestnut St
City: Dallas
Zip: 13892to this:
Name: John Doe
Street: 102 Chestnut St
City: New York
Zip: 13892Name: John Doe
Street: 102 Chestnut St
City: Colorado
Zip: 13892Name: John Doe
Street: 102 Chestnut St
City: Los Angeles
Zip: 13892btw those sed -e commands should be on the same line:
sed -e "s/City: Phi....ado/g" $var > $var.temp

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
doneIf 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

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.

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
}
changecitiesThat 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.

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

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
}
changecitiesSame 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

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

*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

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |