[comp.unix.questions] sed question: lower-to-upper case

klein@mechp10.UUCP (Greg Klein) (08/03/89)

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?

=========================================================================
Greg Klein				| Microcomputer Electronics Corp.
voice:	(206)821-2800 x702		| 12421 Willows Rd, NE
uucp:	...!hplabs!hpubvwa!mechp!klein	| Kirkland WA, USA 98034
=========================================================================

maart@cs.vu.nl (Maarten Litmaath) (08/08/89)

klein@mechp10.UUCP (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?

Sed is not very suitable for this task. :-(
Try the non-interactive variant of `ex' instead:

	ex - file <<\+
	%s/[a-zA-Z]*z[a-zA-Z]*/\U&/g
	x
	+

Explanation: substitute for every (maximal) sequence of letters, containing a
`z', its uppercase (`\U') `equivalent' (`&').
[`&' means: put here (a copy of) the string that was matched.]
-- 
"Mom! Eric Newton broke the day! In 24   |Maarten Litmaath @ VU Amsterdam:
  parts!" (Mike Schmitt in misc.misc)    |maart@cs.vu.nl, mcvax!botter!maart

merlyn@iwarp.intel.com (Randal Schwartz) (08/09/89)

In article <830003@mechp10.UUCP>, klein@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@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/

richl@penguin.USS.TEK.COM (Rick Lindsley) (08/22/89)

In article <4776@omepd.UUCP> merlyn@iwarp.intel.com (Randal Schwartz) writes:
    
    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.
    
Heck. If you're going to use another program, use the one made for just this
purpose and cut down on your typing even more.

    tr a-z A-Z

Rick