Sponsored Content
Full Discussion: wc -l, wc -w,we -c
Top Forums Shell Programming and Scripting wc -l, wc -w,we -c Post 302167571 by otheus on Thursday 14th of February 2008 10:20:02 PM
Old 02-14-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";

 
LOOK(1) 						    BSD General Commands Manual 						   LOOK(1)

NAME
look -- display lines beginning with a given string SYNOPSIS
look [-dfa] [-t termchar] string [file] DESCRIPTION
The look utility displays any lines in file which contain string as a prefix. As look performs a binary search, the lines in file must be sorted (where sort(1) got the same options -d and/or -f that look is invoked with). If file is not specified, the file /usr/share/dict/words is used, only alphanumeric characters are compared and the case of alphabetic char- acters is ignored. Options: -d Dictionary character set and order, i.e. only alphanumeric characters are compared. (On by default if no file specified). -f Ignore the case of alphabetic characters. (On by default if no file specified). -a Use the alternate dictionary /usr/share/dict/web2 -t Specify a string termination character, i.e. only the characters in string up to and including the first occurrence of termchar are compared. The look utility exits 0 if one or more lines were found and displayed, 1 if no lines were found, and >1 if an error occurred. FILES
/usr/share/dict/words the dictionary /usr/share/dict/web2 the alternate dictionary SEE ALSO
grep(1), sort(1) COMPATIBILITY
The original manual page stated that tabs and blank characters participated in comparisons when the -d option was specified. This was incor- rect and the current man page matches the historic implementation. HISTORY
Look appeared in Version 7 AT&T Unix. AVAILABILITY
The look command is part of the util-linux-ng package and is available from ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/. BSD
June 14, 1993 BSD
All times are GMT -4. The time now is 04:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy