Operating System - HP-UX
1834150 Members
2219 Online
110064 Solutions
New Discussion

Finding and replacing word pattern in all files and subdirectories underneath it

 
SOLVED
Go to solution
Shivkumar
Super Advisor

Finding and replacing word pattern in all files and subdirectories underneath it

Hi All,

I want to find a particular word pattern and replace it with other pattern in all files in current directory and files in the subdirectories of the current directory.

Can someone let me know what command and options should be used ?

Thanks,
Shiv
8 REPLIES 8
Pete Randall
Outstanding Contributor

Re: Finding and replacing word pattern in all files and subdirectories underneath it

Something like this:

find . -type f -exec sed 's/foo/bar/g' {} \;

(I think - I didn't have a chance to check it).


Pete

Pete
Kapil Jha
Honored Contributor

Re: Finding and replacing word pattern in all files and subdirectories underneath it

above command
find . -type f -exec sed 's/abc/pqr/g' {} \;
or
find . -type f|xargs sed 's/abc/pqr/g'
would chnge the word you want to change and prrint it on your terminal.
It would not save the files.
Do you want to save the files??
BR,
Kapil+
I am in this small bowl, I wane see the real world......
Shivkumar
Super Advisor

Re: Finding and replacing word pattern in all files and subdirectories underneath it

Yes. I want to make changes in the files and not on the screen.

Thanks,
Shiv
OldSchool
Honored Contributor

Re: Finding and replacing word pattern in all files and subdirectories underneath it

write a simple script to perform the sed and call it from the find command

sed.scr:
#!/bin/sh

tmpname=/tmp/sed.$$

sed 's///g' $1 > $tmpname

cp $tmpname $1

rm $tmpname



then:
find . -type f -exec sed.scr {} \;


ought to be a close (I didn't check or test).

note that sed in hpux doesn't have the ability to do the changes "in-place", which is what requires the copy stuff. GNU variants have that operation, so that you don't need to redirect the output and copy it back.
James R. Ferguson
Acclaimed Contributor

Re: Finding and replacing word pattern in all files and subdirectories underneath it

Hi Shiv:

You can use 'sed' to perform the substitution, but then you would have to redirect the output to a file of a *different* name. Then you have to rename the new output file to be that of the old input file to replace it.

This can be accomodated with:

# cat /usr/local/bin/mything
#!/usr/bin/sh
sed -e s'|string1|string2|g' ${1} > ${1}.new
mv ${1}.new ${1}

...and do:

# find . -type f -exec /usr/local/bin/mything {} \;

Regards!

...JRF...
Bill Hassell
Honored Contributor
Solution

Re: Finding and replacing word pattern in all files and subdirectories underneath it

There is no direct way to insert a change inside a file. That's why I created the chgafile script (attached). Run it once with no options to preview the changes. Then use the options to make the changes. Use it with the find command to locate your files.


Bill Hassell, sysadmin
Dennis Handly
Acclaimed Contributor

Re: Finding and replacing word pattern in all files and subdirectories underneath it

>Bill: There is no direct way to insert a change inside a file.

You can use perl like you did, or you could use ex(1):
for i in $*; do
ex $i < %s:string1:string2:g
wq
EOF
done
James R. Ferguson
Acclaimed Contributor

Re: Finding and replacing word pattern in all files and subdirectories underneath it

Hi (again) Shiv:

As BIll noted, "There is no direct way to insert a change inside a file".

This is true even in the slight-of-hand that Perl plays when it does "in-place" updates as :

# perl -pi.old -e 's/old/new/' file

Under the covers, a temporary file is created and a 'rename()' is performed to save the original copy. The beauty is in that Perl makes hard things easy.

Regards!

...JRF...