Search n replace recursively (693 Views)
Reply
Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 1 of 12 (693 Views)

Search n replace recursively

[ Edited ]

Hi ,

 

/test  with /home/test123

need to replace only /test  by /work  .

 

Only "/test" , it must not leave test123 as it is .

How do i search and replace recursively ?? for many files 

 

 

 

 P.S.This thread has been moved from HP-UX >System Administration to HP-UX > languages-HP Forums Moderator

Acclaimed Contributor
Dennis Handly
Posts: 25,049
Registered: ‎03-06-2006
Message 2 of 12 (683 Views)

Re: Search and replace recursively

You could use "find ... -exec magic-script {} +, where magic-script is a sed or perl script that will change the string.

sed -e 's:/test:/work:' file > file.new

 

>Only "/test", it must not leave test123 as it is.

 

Did you really mean "not" above?  My code will replace only the first "/test" by "/work" but also "/teststuff".

Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 3 of 12 (669 Views)

Re: Search and replace recursively

Oops my mistake.

I meant it must leave test123 as it is.

It must replace /test with /work only in all recursive sub-directory .

 

 

 

Acclaimed Contributor
Dennis Handly
Posts: 25,049
Registered: ‎03-06-2006
Message 4 of 12 (659 Views)

Re: Search and replace recursively

>Oops my mistake.

 

(You can use Post Options > Edit Reply to correct it.)

 

If you only want to change /test, is it always followed by a space?  What are all possible delimiters?

Or is it anything that's NOT [A-Z_a-z0-9]?

 

sed -e 's:/test\([^A-Z_a-z0-9]\):/work\1:' file > file.new

Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 5 of 12 (654 Views)

Re: Search and replace recursively

Hi ,

 

Its only /test  always followd by nothing.

 

its a mountpint replacemnt taken from different server.

 

/test to replaced with /work only  ....many files.

/test123 not to be touched

 

want to do it using perl

 

 

Acclaimed Contributor
Dennis Handly
Posts: 25,049
Registered: ‎03-06-2006
Message 6 of 12 (651 Views)

Re: Search and replace recursively

>Its only /test  always followed by nothing.

 

sed -e 's:/test$:/work:' file > file.new

Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 7 of 12 (641 Views)

Re: Search and replace recursively

Hi Dennis ,

 

How can i achieve this for all subdirectory .

In short a one liner sed or perl would be preferable.

 

 

 

Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 8 of 12 (640 Views)

Re: Search and replace recursively

Also for me this ones working fine ;

 

sed -e 's:/test\([^A-Z_a-z0-9]\):/work\1:' file > file.new

 

 

Acclaimed Contributor
Dennis Handly
Posts: 25,049
Registered: ‎03-06-2006
Message 9 of 12 (637 Views)

Re: Search and replace recursively

>How can I achieve this for all subdirectory?

 

You would take the above find command and create magic-script:

 

#!/usr/bin/ksh

# Replaces /test by /work in all files on the command line

for file in $*; do

   sed -e 's:/test\([^A-Z_a-z0-9]\):/work\1:' $file > $file.new

   if [ $? -eq 0 ]; then

      mv $file.new $file

   fi

done

 

You can probably do it easier in one step in perl.

Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 10 of 12 (635 Views)

Re: Search and replace recursively

Hi ,

 

Can u help me with perl.

Acclaimed Contributor
Dennis Handly
Posts: 25,049
Registered: ‎03-06-2006
Message 11 of 12 (630 Views)
Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 12 of 12 (621 Views)

Re: Search and replace recursively

Hi ,

Already had a look at it before posting here.

Not able to met my requirement.

 

Anyways am looking and trying out all options .

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.