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

replacing text in files w/ a script

Hi.  I working in Windows using Cygwin, a UNIX emulator.

I'm trying to do what I did in UNIX a lot, replace text phrases in files in the current directory and all 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 also tried to break it up into a sed file and a find command:

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?  Thanks!
Gene

0
ugeb
Asked:
ugeb
  • 2
1 Solution
 
sunnycoderCommented:
Hi ugeb,

for filename in `ls -1`
do
         sed 's:/lt/:/lt4/:g' $filename > $filename.temp
         mv $filename.temp $filename
done

OR

find . -name "trial.*" | while read filename
do
         sed 's:/lt/:/lt4/:g' $filename > $filename.temp
         mv -f $filename.temp $filename
done

I have not actually run these scripts, so I recommend that you try it on sample directories first.

Cheers!
Sunnycoder
0
 
ugebAuthor Commented:
Worked like a charm, thanks!
Gene
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi sunnycoder,

For the second form, I'd suggest that find(1) filter on file type and inhibit traversing the subdirectories.  :)


Howdy.....
Kent
0
 
sunnycoderCommented:
Hi Kent ...

I agree .... -type f would be a useful addition

Lists on the left tell me that you have added few more TAs in your folds :)

Cheers!
sunnycoder
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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