Search n replace recursively (546 Views)
Reply
Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 1 of 12 (546 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

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 2 of 12 (536 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".

Please use plain text.
Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 3 of 12 (522 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 .

 

 

 

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 4 of 12 (512 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

Please use plain text.
Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 5 of 12 (507 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

 

 

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 6 of 12 (504 Views)

Re: Search and replace recursively

>Its only /test  always followed by nothing.

 

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

Please use plain text.
Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 7 of 12 (494 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.

 

 

 

Please use plain text.
Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 8 of 12 (493 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

 

 

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 9 of 12 (490 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.

Please use plain text.
Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 10 of 12 (488 Views)

Re: Search and replace recursively

Hi ,

 

Can u help me with perl.

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 11 of 12 (483 Views)
Please use plain text.
Regular Advisor
coollllllllllll
Posts: 140
Registered: ‎12-28-2012
Message 12 of 12 (474 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 .

Please use plain text.
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