Complete.Org: Mailing Lists: Archives: discussion: November 2000:
[aclug-L] Re: sed or awk??? or better way to do.
Home

[aclug-L] Re: sed or awk??? or better way to do.

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: "'discussion@xxxxxxxxx'" <discussion@xxxxxxxxx>
Subject: [aclug-L] Re: sed or awk??? or better way to do.
From: "Wilner, Alden" <awilner@xxxxxxxx>
Date: Fri, 3 Nov 2000 16:26:27 -0600
Reply-to: discussion@xxxxxxxxx

> -----Original Message-----
> From: Maverick Ieong [mailto:mluvw47@xxxxxxxxx]
> Sent: Friday, November 03, 2000 11:39 AM...
> I would like to read in a text file. for example,
> //this is a text.file
> I am old, and I am bad, badly,
> old stuff and bad stuff
> ...................
> ..............
> 
> 
> and replace all the occurence the word accordingly to
> on the same text file, like
> //this is a text.file
> I am new, and I am good, badly,
> new stuff and good stuff
> .....................
> ...............
> 
> 
> note, I only want to replace the exactly accordingly,
> for example, the word "badly" didn't replace by
> "goodly".

One way to do it:

perl -p -e "s/\bbad\b/good/g"

-p says, basically "behave like awk" (i.e., assume a while loop around the
perl code, and print)
-e says "execute this here perl script"
s is the substitute command.
\b matches only at the boundary between a word and a nonword
g means 'globally on the line." -- that is, if the word's on the line,
change it both times.

If you want to change more than one word, you can stack the subs:

perl -p -e "s/\bbad\b/good/g;s/\bold\b/good/g"

...but if you're going to get much more complicated than 2 or 3 word
replacements you'll want to stick the perl program in a file, place the
substitution words in a tab or comma-delimited data file, probably allow for
multiple substitution string capabilities, e.g.

bad,naughty,evil:good
old,ancient,archaic:new

Then you'll have to post the thing on the CPAN , and then someone'll
probably request that you rewrite it with an object-oriented interface, and
that'll be a real pain, so maybe you'd just better stick with sed... ;-)

Sincerely,
Alden Wilner -- alden.wilner@xxxxxxxx
"The Good Old Days were when you were too ignorant to know any better."
-- Oleta Adams




-- This is the discussion@xxxxxxxxx list.  To unsubscribe,
visit http://tmp2.complete.org/cgi-bin/listargate-aclug.cgi


[Prev in Thread] Current Thread [Next in Thread]
  • [aclug-L] Re: sed or awk??? or better way to do., Wilner, Alden <=