wc -l, wc -w,we -c


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting wc -l, wc -w,we -c
# 1  
Old 02-14-2008
wc -l, wc -w,we -c

Hi

I have the following in the file

I love scripting
I love Unix

I know how to use wc in unix to figure out words, characters, lines

but how about Perl, what approach should I take?
# 2  
Old 02-15-2008
Quote:
Originally Posted by james94538
I know how to use wc in unix to figure out words, characters, lines

but how about Perl, what approach should I take?
Lots of different approaches. I'll try some that won't win any obfuscation contests:

Lines:
Code:
my $cnt=0; 
while ($_=<>) { $cnt++; } 
print $cnt,"\n";

Words: (loosely defined as whitespace-delimited tokens)
Code:
my $cnt=0;
while ($_=<>) { $cnt+= scalar( split ); }
print $cnt,"\n";

Words: (defined as whitespace-delimited tokens containing a letter)
Code:
my $cnt=0;
while ($_=<>) { $cnt+= s/\b\S*[a-zA-Z]+\S*\b//; }
print $cnt,"\n";

Characters: (not including line-feeds, but including all others, ie, whitespace)
Code:
my $cnt=0;
while ($_=<>) { chop; $cnt+= length($_); }
print $cnt,"\n";

Characters: (not including whitespace)
Code:
my $cnt=0;
while ($_=<>) { chop; s/\s//g; $cnt+= length($_); }
print $cnt,"\n";

Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question