Counting characters with unix utilities

164 views
Skip to first unread message

Dale Worley

unread,
Sep 25, 1990, 11:10:14 AM9/25/90
to

X-Name: Randal Schwartz

perl -ne '$c += tr/A/A/; print $c if eof;' file

Doesn't this fail if the file is empty?

Dale Worley Compass, Inc. wor...@compass.com
--
Using MS-DOS on a '386 is like using a Formula 1 race car to do your
grocery shopping.

Randal Schwartz

unread,
Sep 25, 1990, 1:12:37 PM9/25/90
to
In article <1990Sep25.1...@uvaarpa.Virginia.EDU>, worley@compass (Dale Worley) writes:
|
| X-Name: Randal Schwartz
|
| perl -ne '$c += tr/A/A/; print $c if eof;' file
|
| Doesn't this fail if the file is empty?

Ooops. I'll be out painting fence-posts for a while, sorry. :-)
--
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III |
| mer...@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

Dale Worley

unread,
Sep 26, 1990, 8:50:33 AM9/26/90
to

From: mer...@iwarp.intel.com (Randal Schwartz)

| perl -ne '$c += tr/A/A/; print $c if eof;' file
|
| Doesn't this fail if the file is empty?

Ooops. I'll be out painting fence-posts for a while, sorry. :-)

It seems like it would be useful to be able to prescribe prolog and
epilog code for -n (and -p) loops. AWK does this with BEGIN and END.
Then it would be simple:

perl -ne '$c += tr/A/A/' -xxx 'print $c' file

Dale Worley Compass, Inc. wor...@compass.com
--

Give yourself over to absolute pleasure
Swim the warm waters of sins of the flesh
Erotic madness beyond any measure
And sensual daydreams to treasure... forever.
-- Rocky Horror Picture Show

Larry Wall

unread,
Sep 26, 1990, 2:21:45 PM9/26/90
to
In article <1990Sep26.1...@uvaarpa.Virginia.EDU> wor...@compass.com writes:
:
: From: mer...@iwarp.intel.com (Randal Schwartz)

:
: | perl -ne '$c += tr/A/A/; print $c if eof;' file
: |
: | Doesn't this fail if the file is empty?
:
: Ooops. I'll be out painting fence-posts for a while, sorry. :-)
:
: It seems like it would be useful to be able to prescribe prolog and
: epilog code for -n (and -p) loops. AWK does this with BEGIN and END.
: Then it would be simple:
:
: perl -ne '$c += tr/A/A/' -xxx 'print $c' file

It hardly seems worth it to add a new switch when you can say

perl -e 'while(<>){$c += tr/A/A/;}print $c' file

or

perl -ne '$c += tr/A/A/; print $c if eof()' file

or

perl -e 'undef $/; $_ = <>; print tr/A/A/' file

or even

perl -e '$c += tr/A/A/ while <>; print $c' file

Although the Perl Slogan is There's More Than One Way to Do It, I hesitate
to make 10 ways to do something. :-)

Larry

Reply all
Reply to author
Forward
0 new messages