ugeb
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
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
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.
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
export TMPIN = "$FILE".tmp
cp $FILE $TMPIN
sed [command] $TMPIN > $FILE
rm $TMPIN
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.
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
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
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.
find /path -name "*" - exec sedsript.sh {} \;
should be fine.
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works great, thank you:)
sed -e [the rest]
works in Cygwin.