Computing.Net > Forums > Unix > Need help with a search and replace logi

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Need help with a search and replace logi

Reply to Message Icon

Name: Jaggar
Date: June 5, 2002 at 13:58:30 Pacific
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.



Sponsored Link
Ads by Google

Response Number 1
Name: Panic
Date: June 5, 2002 at 14:36:31 Pacific
Reply:

Use the vi editor it´s ugly but have many funcions, type
man vi
for complete(many pages) reference


0

Response Number 2
Name: Panic
Date: June 5, 2002 at 14:40:49 Pacific
Reply:

PD i have a pdf book for shell programming it´s good if you want it tell me


0

Response Number 3
Name: jaggar
Date: June 5, 2002 at 16:22:27 Pacific
Reply:

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.


0

Response Number 4
Name: LANkrypt0
Date: June 6, 2002 at 05:16:55 Pacific
Reply:

Sed should work fine. I did:
sed -e "s/City: New York/City: Philadelphia/g" test2

And that worked just fine.


0

Response Number 5
Name: LANkrypt0
Date: June 6, 2002 at 06:54:51 Pacific
Reply:

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



0

Related Posts

See More



Response Number 6
Name: LANkrypt0
Date: June 6, 2002 at 07:16:08 Pacific
Reply:

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


0

Response Number 7
Name: Jaggar
Date: June 6, 2002 at 08:56:14 Pacific
Reply:

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.


0

Response Number 8
Name: LANkrypt0
Date: June 6, 2002 at 10:30:45 Pacific
Reply:

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.


0

Response Number 9
Name: Jaggar
Date: June 6, 2002 at 12:44:25 Pacific
Reply:

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


0

Response Number 10
Name: LANkrypt0
Date: June 6, 2002 at 13:03:34 Pacific
Reply:

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



0

Response Number 11
Name: LANkrypt0
Date: June 6, 2002 at 13:20:01 Pacific
Reply:

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



0

Response Number 12
Name: LANkrypt0
Date: June 6, 2002 at 13:23:46 Pacific
Reply:

*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


0

Response Number 13
Name: Jaggar
Date: June 6, 2002 at 18:57:22 Pacific
Reply:

Thanks so much, I will try this... ur very helpfull!!!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Need help with a search and replace logi

Search And Replace With Sed www.computing.net/answers/unix/search-and-replace-with-sed/3845.html

Help search and replace www.computing.net/answers/unix/help-search-and-replace/6886.html

Need help with a unix script www.computing.net/answers/unix/need-help-with-a-unix-script/4288.html