sed question: lower-to-upper case

Randal Schwartz merlyn at iwarp.intel.com
Thu Aug 10 00:39:50 AEST 1989


In article <830003 at mechp10.UUCP>, klein at mechp10 (Greg Klein) writes:
| I would like to use sed to change certain (not all) words in a text file
| from lower case to upper case.  For example, the selected words to be
| changed might be all words that contain one or more lower case z's in
| them (like 'zebra').  Any ideas?

Yeah, don't use sed.

#ifdef small_flame
why do so many people wanna use the wrong UN*X tool?  Sigh.
#endif small_flame

First, I'd recommend using Perl, just cause it seems to do things like
this pretty well.  If you *insist* on using sed because of its
Turing-machine-equivalent power, you will need to do something
elaborate with the 'hold' space and the 'y' command.  Good luck!  (No,
I'm not going to post a 50-line sed script to do it... sorry. :-)

Another solution if you toss the idea of using Perl would be to create
the appropriate patterns with lex, and put in a little bit of C.
Something along the order of:

%%
[a-z]*z[a-z]*		{ chp = yytext; while(*chp) putchar(toupper(*chp++)); }
.|\n			{ ECHO; }

(WARNING: probably bad code there, this was just an UNTESTED example...)

But, get Perl.  Then you can say something like:

perl -pe 's|\w*z\w*|($& =~ y/a-z/A-Z/),$&|eg;'

and do what you want in *one* line.

Just another Perl hacker,
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel, Hillsboro, Oregon, USA                           |
| merlyn at iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/



More information about the Comp.unix.questions mailing list