Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 879
  • 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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
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

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

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