Link to home
Start Free TrialLog in
Avatar of boucherc
boucherc

asked on

scripting find grep replace in shell

Don't know if it matters, but using bash shell, I'm trying to do a find that will find all files with a patricular name that contains a particular text and do a vi substitute on that text.

Something like:

find . -name "MANIFEST.MF" -exec `vi - {} << EOD :%s/SOME_TEXT/NEW_TEXT/g :wq EOD` '{}' \;

The syntax probably isn't even remotely correct, but you get the gist of it. If you know of a better way, that's acceptible as well.
Avatar of fseto
fseto

Instead of using vi, use sed...

find .  -name "MANIFEST.MF"  -exec sed  's/oldpatt/newpatt/g'  {} \;
Avatar of omarfarid
Hi,

Vi  is an interactive editor. You may use other line editors like ed or ex.

e.g.

ed file <END
1,$ s/old/new/g
w
q
END


Avatar of boucherc

ASKER

fseto's response finds them all, and looks to replace them, but after execution when I do a find -exec grep "oldpatt", it still exists in the files it supposedly changed.
omarfarid, it needs to be a single line command.
find .  -name "MANIFEST.MF"  -exec sed -i.bak 's/oldpatt/newpatt/g'  {} \;
ozo, the above resulted in the following for each file that was attempted to be edited:

sed: illegal option -- i

Remember, this is Solaris 2.9, so some UNIX rules might not apply.
Oops, forgot to pipe the output back to the orig file.  BTW, make sure you test this!  As if sed bombs, it will overwrite your existing data!  Instead of output to {}, you can use {}.new.  And then check for correctness.

find .  -name "MANIFEST.MF"  -exec sed  's/oldpatt/newpatt/g'  {} >! {} \;
still didn't substitute them.
it actually pipes it out to a file names !, which has the substituted text.
do u have permissions to change the files?
Hi,

boucherc:

Why don't you take my script and develop it furhter :)

Here a modified one:

Call it myscript:

export count=$#
while test $count -gt 0
do
ed $1 <END
1,$ s/old/new/g
w
q
END
shift
done

make the script executable:

chmod +x /path/to/myscript

Then use it with find

find . -name "MANIFEST.MF" -exec /path/to/myscript {} \;
omar

I tried your version as a script and got the following:

count=1: is not an identifier

and to talmash, yes, I do have the permissions to change the file.
Hi,

Which shell are you using? Can you till which line number gave the error?

Any way my script is incomplete. Please add the line below after END and before shift:

count=`expr $count - 1`

Also,

you may change export count=$# to

count=$#
export count
I'm using bash, but wrote the script as bourne.
after changing the export line to be 2 separate lines, I no longer get the "count=1: is not an identifier" error.

I named my script as change_manifest and here are its contents:

#!/bin/sh

count=$#
export count
while test $count -gt 0
do
ed $1 <END
1,$ s/jaleksyn/${LOGNAME}/g
w
q
END
shift
done

I didn't add the count=`expr $count - 1` line yet, but now I get the following when running
find . -name MANIFEST.MF -exec change_manifest {} \;

change_manifest: END: cannot open
change_manifest: 1,$: not found
 10:39am  up 25 day(s),  3:33,  2 users,  load average: 0.16, 0.17, 0.17
User     tty           login@  idle   JCPU   PCPU  what
root     console       2Nov0716days     23         -sh
dynamo   pts/1        10:20am                      w
change_manifest: q: not found
change_manifest: END: not found
change_manifest: END: cannot open
change_manifest: 1,$: not found
 10:39am  up 25 day(s),  3:33,  2 users,  load average: 0.16, 0.17, 0.17
User     tty           login@  idle   JCPU   PCPU  what
root     console       2Nov0716days     23         -sh
dynamo   pts/1        10:20am                      w
change_manifest: q: not found
change_manifest: END: not found
change_manifest: cannot shift


ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

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
BINGO! This was just what I was looking for.

Thank you.
Hi,

Sorry boucherc

It seams my copy and paste did copy properly. It should have been << END