Reimplement the wc coreutil This challenge is similar to this old
one, but with some unclear
parts of the spec hammered out and less strict I/O requirements.



Given an input of a string consisting of only printable ASCII and newlines,
output its various metrics (byte, word, line count).

The metrics that you must output are as follows:


Byte count. Since the input string stays within ASCII, this is also the
character count.
Word count. This is wc's definition of a "word:" any sequence of
non-whitespace. For example, abc,def"ghi" is one "word."
Line count. This is self-explanatory. The input will always contain a
trailing newline, which means line count is synonymous with "newline
count." There will never be more than a single trailing newline.


The output must exactly replicate the default wc output (except for the file
name):

llama@llama:~$ cat /dev/urandom | tr -cd 'A-Za-z \n' | head -90 &gt; example.txt
llama@llama:~$ wc example.txt
  90  165 5501 example.txt


Note that the line count comes first, then word count, and finally byte count.
Furthermore, each count must be left-padded with spaces such that they are all
the same width. In the above example, 5501 is the "longest" number with 4
digits, so 165 is padded with one space and 90 with two. Finally, the
numbers must all be joined into a single string with a space between each
number.

Since this is code-golf, the shortest code in bytes will win.

(Oh, and by the way... you can't use the wc command in your answer. In case that wasn't obvious already.)

Test cases (\n represents a newline; you may optionally require an extra
trailing newline as well):

"a b c d\n" -&gt; "1 4 8"
"a b c d e f\n" -&gt; " 1  6 12"
"  a b c d e f  \n" -&gt; " 1  6 16"
"a\nb\nc\nd\n" -&gt; "4 4 8"
"a\n\n\nb\nc\nd\n" -&gt; " 6  4 10"
"abc123{}[]()...\n" -&gt; " 1  1 16
"\n" -&gt; "1 0 1"
"   \n" -&gt; "1 0 4"
"\n\n\n\n\n" -&gt; "5 0 5"
"\n\n\na\nb\n" -&gt; "5 2 7"
Perl, 49 bytes

Added +3 for -an0

Input on STDIN or 1 or more filenames as arguments.
Run as perl -an0 wc.pl

wc.pl:

/\z/g;pos=~//;printf"%@+d %@+d $`
",y/
//,~~@F


Explanation:

-n0      slurps the whole input into $_ and says we will do our own printing
-a       tells perl to split the input on whitespace into array @F
/\z/g    Matches the absolute end of the input. g modifier so the position 
         is remembered in pos which will now contain the input length
pos=~//  An empy regex repeats the last succesful match, so /\z/ again.
         After that $` will contain the the number of input characters and
         the array @+ will contain the length of this number
printf   All preparation is complete, we can go print the result
"%@+d"   will become e.g. %6d if the number of characters is a number of
         length 6, so lines and words will get printed right aligned 
         in a field of length 6.
$`       $` we can directly interpolate since it won't contain a %
y/\n//   Count the number of newlines in $_
~~@F     The array of words @F in scalar context gives the number of words