Changing from capital letters to small letters using perl

V

Venugopal

Hi,

Is there an easy way to change all capital letters in a text file
to small letters using Perl? (Or any other tool!)


Thanking in advance,
Venugopal
 
I

Iain Chalmers

Hi,

Is there an easy way to change all capital letters in a text file
to small letters using Perl? (Or any other tool!)

Yep, perl has a "lower case" function called "lc"

perldoc -f lc

A short example:

#!/usr/bin/perl -w
use strict;
undef $/; #enable 'slurp' mode
open(FILE,'filename.txt') || die $!;
$_=<FILE>; #read entire file into $_
close FILE;
print lc; #print lowercase version (lc defaults to $_)
__END__

cheers,

big (yeah yeah, theres no need to not do it line by line...)
 
I

Iain Chalmers

Gunnar Hjalmarsson said:


which directs you to:

"If you want to map strings between lower/upper cases, see
perlfunc/lc and perlfunc/uc"

out of interest, I did this:

#!/usr/bin/perl -w
use strict;
use Benchmark;

my $text='aBcDeFgHiJkLmNoPqUsTuVwXyZ';
my $result;

timethese(1000000, {
'lc' => sub {$result=lc($text)},
'tr' => sub {($result = $text)=~ tr/A-Z/a-z/;},
});

and got this:

Benchmark: timing 1000000 iterations of lc, tr...
lc: 0 wallclock secs ( 1.67 usr + 0.00 sys = 1.67 CPU)
@ 598802.40/s (n=1000000)
tr: 3 wallclock secs ( 2.40 usr + 0.00 sys = 2.40 CPU)
@ 416666.67/s (n=1000000)


which makes lc look quite a bit quicker. If I change it so

my $text='aBcDeFgHiJkLmNoPqUsTuVwXyZ' x 100;

I get:

Benchmark: timing 1000000 iterations of lc, tr...
lc: 64 wallclock secs (46.09 usr + 0.00 sys = 46.09 CPU)
@ 21696.68/s (n=1000000)
tr: 78 wallclock secs (55.29 usr + 0.00 sys = 55.29 CPU)
@ 18086.45/s (n=1000000)

And, of course, lc knows about locale where tr doesn't...

big
 
G

Gunnar Hjalmarsson

Iain said:
which directs you to:

"If you want to map strings between lower/upper cases, see
perlfunc/lc and perlfunc/uc"

Maybe. I thought this would be an appropriate task for a one liner,
and I made this work:

$ perl -p -e 'tr/A-Z/a-z/' < oldfile.txt > newfile.txt

but didn't find any way to do it using lc(). (As a W98 user I almost
never play with one liners...)
 
J

Jürgen Exner

Roland said:
$text =~ y/A-Z/a-z/;

Which of course works only for a small subset of letters even in
ISO-Latin-1, not to mention other character sets.
Better to use the proper function lc().

jue
 
S

Steve Grazzini

Gunnar Hjalmarsson said:
I thought this would be an appropriate task for a one liner,
and I made this work:

$ perl -p -e 'tr/A-Z/a-z/' < oldfile.txt > newfile.txt

but didn't find any way to do it using lc().

% perl -pe '$_ = lc' old > new
 
T

Tassilo v. Parseval

Also sprach Jürgen Exner:
Which of course works only for a small subset of letters even in
ISO-Latin-1, not to mention other character sets.
Better to use the proper function lc().

Plus 'use locale', otherwise lc() will just handle those characters in
the ASCII character set.

Tassilo
 
A

Alan J. Flavell

Hey, didn't the group just have this discussion, a few days back?
Plus 'use locale', otherwise lc() will just handle those characters in
the ASCII character set.

That's clear enough if you're working in one 8-bit locale.

See the earlier discussion re strange interactions between "use
locale" and Unicode support, if you go beyond that.
 
T

Tassilo v. Parseval

Also sprach Alan J. Flavell:
Hey, didn't the group just have this discussion, a few days back?

Hom come that I somehow assumed that you, Alan, would pop in and join
this thread? :)
That's clear enough if you're working in one 8-bit locale.

See the earlier discussion re strange interactions between "use
locale" and Unicode support, if you go beyond that.

No, I absolutely wont. Localization stops for me when it involves more
than eight bits. I am clueless to an almost criminal extent when it
comes to Unicode. :)

Which is why I tend to read all your articles on this topic here in this
group more carefully and in-depth since they always tell me things I
wasn't in the least aware of before.

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top