Operating System - HP-UX
1827279 Members
3374 Online
109717 Solutions
New Discussion

Re: find and replace script

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

find and replace script

attached is a script that'll find and replace text in files recursively from the directory it's run from.

In my case, it's mainly written to replace hardcoded hostnames in config files but might be useful to others. Of course it's not too sensitive on files it's looking in, but is a better alternative to vi'ing lots of files individually.

Comments and/or corrections/improvements always welcome.

Later,
Bill
It works for me (tm)
5 REPLIES 5
Edward Alfert_2
Respected Contributor

Re: find and replace script

THANKS BILL!

I was manually changing some webpages using vi just last night. What a tedious process. I only had a console and couldn't use a windows machine.

This will definitely come in handy...

thanks again for sharing.
"Do what you love and you will never work a day in your life." - Confucius
Robin Wakefield
Honored Contributor

Re: find and replace script

Bill,

nit-picking really, but here's a few comments:

i) the awk's not really needed because you're executing the cut
ii) the second if block contains a "mv", I reckon this should be a "cp", but...
iii) ...you could combine the 2 if statements into one to cut down on duplicate code (it's running both anyway for "ascii text" files).

Rgds, Robin.
Bill McNAMARA_1
Honored Contributor

Re: find and replace script

Yea Robin, you're right,

First point, yep, that's what I get for adding to it bit by bit..

the text is there for command text, ascii doesn't show up on my script files...
Although for duplication I couldn't get the || (or) in my if statement, i didn't want to spend too long looking to clean it up, but should really..

Probably should change to cp, but, I don't want to delete files seperately..

Bill
It works for me (tm)
Carlos Fernandez Riera
Honored Contributor

Re: find and replace script


I`ve modified your file.

Unchecked. Could contain syntax errors.
unsupported
Carlos Fernandez Riera
Honored Contributor
Solution

Re: find and replace script

Try this version better..
unsupported