Link to home
Start Free TrialLog in
Avatar of ugeb
ugebFlag for United States of America

asked on

replace text with cygwin or some script

I'm trying to do what I did in UNIX a lot, replace text phrases in files in the current directory and subdirectories.   I have Cygwin, but can't get the syntax correct.  If there is a better shell or a better way, I'd like to know that too .

I like emacs, so ideally I'd do this in an emacs shell on Windows XP.

the sed command I'd use has the form:

sed 's/\/lt\//\/lt4\//g' * > ????                           ;replace "/lt/" with "/lt4/"  and do this with files in all subdirectories too

So, for each file found it should make that replacement and put it back in the same file name.  I've never done a script in Windows, only c-shell in UNIX, so I'm not sure of the methodology or language.

Thanks for the help.
Gene
Avatar of jkr
jkr
Flag of Germany image

IIR making that

sed -e [the rest]

works in Cygwin.
Avatar of ugeb

ASKER

Sorry, don't understand your post.

My biggest problem is getting sed to replace the text and put it in the same file, otherwise it just goes to stdout. Doing

sed .....   *.cpp > ' don't know what to put here'

that's what I'm asking about.

thanks.

Ah, got it. Yes, you're right, that is a bit odd, but why not copying the original to a temp file first and have sed process that? I.e.

export TMPIN = "$FILE".tmp
cp $FILE $TMPIN
sed [command] $TMPIN > $FILE
rm $TMPIN
Avatar of ugeb

ASKER

Yes, that works for a single file, but how do I get it to repeat this process for a thousand files in multiple subdirectories?  That's why I need some sort of foreach loop or something.

Thanks.
How are you looking them up? One easy thing would be to

find /path -name "*.toreplace" - exec sedsript.sh {} \;

with 'sedscript.sh' being

export FILE = $1
export TMPIN = "$FILE".tmp
cp $FILE $TMPIN
sed [command] $TMPIN > $FILE
rm $TMPIN

Avatar of ugeb

ASKER


sed 's/\/lt\//\/lt4\//g'      *


I want to just get all files "*" on the sed command line.


Then

find /path -name "*" - exec sedsript.sh {} \;

should be fine.

Avatar of ugeb

ASKER

Are you sure that worked for you in cygwin?

My command line is:

find . -name "trial.*" -exec sedscript.sh {} \;

and I got this output:
[begin output]
starting
export: =: bad variable name
File = ./trial.cpp
export: =: bad variable name
cp missing file operand
Try cp <snip>
sedscript.sh: cannot create : directory nonexistant
rm missing operand
[end output]

my sedscript.sh file looks like this:

echo "starting"
export FILE = $1
echo "File = " $1
export TMPIN = "$FILE".tmp
cp $FILE $TMPIN
sed -e 's/\/lt\//\/lt4\//g' $TMPIN > $FILE
rm $TMPIN

so, I'm pretty much getting an error on every line.  Ideas?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

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
Avatar of ugeb

ASKER

Works great, thank you:)