Avatar of ugeb
ugebFlag for United States of America

asked on 

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

Unix OS

Avatar of undefined
Last Comment
sunnycoder
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ugeb
ugeb
Flag of United States of America image

ASKER

Worked like a charm, thanks!
Gene
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi sunnycoder,

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


Howdy.....
Kent
Avatar of sunnycoder
sunnycoder
Flag of India image

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
Unix OS
Unix OS

Unix is a multitasking, multi-user computer operating system originally developed in 1969 at Bell Labs. Today, it is a modern OS with many commercial flavors and licensees, including FreeBSD, Hewlett-Packard’s UX, IBM AIX and Apple Mac OS-X. Apart from its command-line interface, most UNIX variations support the standardized X Window System for GUIs, with the exception of the Mac OS, which uses a proprietary system.

33K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo