Summary: Help for upper-casing keywords

Rohit Mehrotra rohit at dmdev.UUCP
Tue Feb 12 02:24:07 AEST 1991


> ORIGINAL QUESTION:

>I want to convert a list of keywords (about 150) into upper-case
>                                     ^^^^^^^^^^^
>where ever they occur in a text file, i.e. for ex: wherever say "update"
>or "Update" occurs as a full word change it to "UPDATE".
>			 ^^^^^^^^^^^^^^^^
>Is their a PERL,SED,AWK script out their that would do this for me.

Thanks a lot to all who EMAILed me an answer. The answers basically fall into
thre categories: 

A) Using Sed / Awk script
B) Using tr script.
C) Using a PERL script

A) The ones for sed and awk did not solve my purpose as :

1) Because of the number of keywords the sed scripts were exceeding the the
   sed's limit of total number of command text, number of commands.

2) There was no way do replacements for complete words only, i.e for the above
   example I did not want word "updating" to be capitalized.

B) The ones with the tr scrips were just capitalizing the full text and not
   exclusively the keywords.

C) There were a few PERL scripts EMAILed and well they serve the purpose in its
   completely. I am including the responses of the ones that I found really 
   interesting, however all of them did essentially come up with the same 
   regular expression.

Thanks a lot to all of you guys.

rohit

_____________________________________________________________________________
From: merlyn at iwarp.intel.com

#!/usr/bin/perl -p

$code = <<X;
while (<>) {
X

while (<DATA>) {
        chop;
        $code .= <<"X";
        s/\\b$_\\b/$_/ig;
X
}

$code .= <<X;
	print;
}
X

eval $code;  die $@ if $@;
__END__
UPDATE
SMURF
HAPPY

__________________________________________________________________________
From: me at ebay.sun.com

#! /usr/local/bin/perl
 
%keywords = (
    'fred',     'FRED',
    'jane',     'JANE',
    'george',   'GEORGE',
);

while (<>) {
    for $keyword (keys (%keywords)) {
        s/\b$keyword\b/$keywords{$keyword}/gi;
    }
    print $_;
}
_______________________________________________________________________________
From: tchrist at pixel.convex.com

    #!/usr/bin/perl
    $WORDS = shift 	|| die "usage: $0 wordlist [files ...]\n";
    open WORDS 		|| die "can't open $WORDS $!";
    $code = "while (<>) {\n    study;\n";
    while (<WORDS>) {
	chop;
	s/(\W)/\\$1/g;
	($lhs = $_) =~ tr/A-Z/a-z/;
	($rhs = $_) =~ tr/a-z/A-Z/;
	$code .= "    s/\\b$lhs\\b/$rhs/gi;\n";
    } 
    $code .= "    print;\n}\n";
    #print STDERR $code;
    eval $code;
    die $@ if $@;
_______________________________________________________________________________
-- 
Rohit Mehrotra
Fleet Credit Corporation
8325 NW 53rd St, Miami, Fl 33166.
E-MAIL Address uunet!dmdev!rohit VOICE 1-(305)-477-0390 Ext 469



More information about the Comp.unix.programmer mailing list