• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 885
  • Last Modified:

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
0
ugeb
Asked:
ugeb
  • 5
  • 5
1 Solution
 
jkrCommented:
IIR making that

sed -e [the rest]

works in Cygwin.
0
 
ugebAuthor Commented:
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.

0
 
jkrCommented:
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
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
ugebAuthor Commented:
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.
0
 
jkrCommented:
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

0
 
ugebAuthor Commented:

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


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


0
 
jkrCommented:
Then

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

should be fine.

0
 
ugebAuthor Commented:
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?
0
 
jkrCommented:
Hm, I made that

#!/bin/sh
echo "starting"
INFILE=$1
echo "File = " $1
TMPIN=$INFILE.tmp
cp $INFILE $TMPIN
sed -e 's/\/lt\//\/lt4\//g' $TMPIN > $INFILE
rm $TMPIN

and it works fine.
0
 
ugebAuthor Commented:
Works great, thank you:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now