Operating System - Linux
1748185 Members
4382 Online
108759 Solutions
New Discussion

Re: scripting help with find

 
SOLVED
Go to solution
Andres_13
Respected Contributor

scripting help

Hi there,

 

I have the following shell in a RHEL 4 Server:

 

export DIR=/u01
for dir in `find $DIR -type d`;
do
grep pattern $dir/*;
done

As the above script gives thousands of files I'm looking for help to actually find the pattern and also change it for another string.

 

Any help will be appreciated

6 REPLIES 6
Steven Schweda
Honored Contributor

Re: scripting help

 
Dennis Handly
Acclaimed Contributor

Re: scripting help with find

>As the above script gives thousands of files

 

1000s of files as in you want to automate it?  Or you just want to limit it?

 

If you have a shell pattern match instead of a grep RE, you can use -name:

 

DIR=/u01
find $DIR -type f -name "pattern" | while read file; do
   # do something with filename ...
done

Andres_13
Respected Contributor

Re: scripting help with find

I want to automate it. Let me explain a bit more the current state:

 

I have just virtualized a production environment (RHEL 4 running oracle application server). It's a production environment for the purpose of making a development environment. It's something I never did before and also I don't know how OAS works.

So I thought it it would be easier to change the virtual server hostname and replace the original server name for the virtual of all files containing the name of the production system.

Answering your question I want to automate the process of find all the files containing the prod server hostname, edit each one of them in order to get all of these files "pointing" to the new virtual server's hostname.

Andres_13
Respected Contributor

Re: scripting help with find

Maybe something like this:

 

for dir in `find $OAS -type d`;
do
FILE=`grep $SOURCE_HOSTNAME $dir/*`;
if [ $FILE ]; then
/bin/cat - << EOF | /bin/ed -s $FILE
1,$ s/$SOURCE_HOSTNAME/$TARGET_HOSTNAME/g
w $FILE
q
EOF
FILE=\0
fi
done

 

But above doesn't work yet

Dennis Handly
Acclaimed Contributor
Solution

Re: scripting help with find

>find all the files containing the prod server hostname, edit each one of them in order to get all of these files "pointing" to the new virtual server's hostname.

 

DIR=/u01
find $DIR -type f | while read file; do

   fgrep -q "$SOURCE_HOSTNAME" $file

   if [ $? -eq 0 ]; then

      sed "s/$SOURCE_HOSTNAME/$TARGET_HOSTNAME/g" $file > $file.new &&

         mv $file.new $file   # permission & ownership possibly changed

   fi

done

Andres_13
Respected Contributor

Re: scripting help with find

Thank's for your support dennis, that did the trick!.

 

Regards!