Link to home
Start Free TrialLog in
Avatar of n00b0101
n00b0101

asked on

unix find and replace recursively

Hopefully, this will be a quick one for someone here... I need to find and replace a string recursively in unix.  

Normally, I use:

 
   perl -e "s/term/differenterm/g;" -pi $(find path/to/DIRECTORY -type f)

Open in new window


But, the string I need to replace contains slashes, and I'm not sure how to escape them?

So, I need to do:

    perl -e "s/FIND/REPLACE/g;" -pi $(find path/to/DIRECTORY -type f)

Open in new window

where FIND = '/string/path/term'
and REPLACE = '/string/path/newterm'
Avatar of farzanj
farzanj
Flag of Canada image

You can use

s###;

s#/string/path/term#/string/path/newterm#;

OR
s{/string/path/term}{/string/path/newterm};
Avatar of n00b0101
n00b0101

ASKER

I tried both of those, but it returns "Illegal variable name".

perl -e "s#/string/path/term/#/string/path/newterm/#" -pi $(find ./* -type f )
Illegal variable name.

Open in new window

What exactly are you trying to do?
"
So, I need to do:

   
perl -e "s/FIND/REPLACE/g;" -pi $(find path/to/DIRECTORY -type f)

Open in new window

                                 
where FIND = /string/path/term
and REPLACE = /string/path/newterm
"
perl -pi.bak -e 's/(\/string\/path\/)term/$1newterm/g;'  $( find /path/to/directory -type f)
The -i option requires a suffix to a maintain a backup.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial