Phoenix.pm: Code problem

Phaedrus phaedrus at endless.org
Tue Oct 12 18:34:52 CDT 1999


> OK, OK!!  I have to fess up that the $size assignment in Ron's code is 
> totally and completely greek to me...
> 
> $size = `wc -w $out_file | tr -s " " | cut -f1 -d " "` ;
> 
> Can someone go through that assignment statement,  character by character and 
> decipher it for me (and I'm sure others)?
> 

Took me a second too. I didn't know there was a 'cut' command. I usualy
use 'sed' for such things =)
First: ` stuff ` means, execute "stuff" in a Unix shell, and return the
output from the commands.
Second, |'s are pipeline operators (you probably know this much, sorry):
the output from one command is sent into the input of the next command.
The first command, wc, is "word count". The -w flag tells it to count
words (instead of characters or lines). It returns something like:
   199   outfilename
This has more spaces then it needs, make it hard to process. The author
wanted only the number from this, so he runs it through tr -s (translate)
to nuke spaces that occured next to other spaces (if I remember
correctly?). The output of that is:
 199 outfilename
Cut is being told, then, to take the first field, where the space " " is
considered the field delimiter.
The problem is, since it starts with a space, which is the field
seperator, there is a null field - a field with nothing in it - as far as
cut is concerned, so it returns this null field, as it was asked for the
first field. Had it been asked for the second field, it would have
returned the number, 199.

This is more typical of bourne shell programming/csh programming/shell
programming then Perl programming. Shell script usualy makes use of dozens
of little unix utilities, piping input from one to the next, where as Perl
is obviously all one interpriter. Hope that helps. I think I had too much
caffinee, forgive my enthusiasm =)
-=scott





More information about the Phoenix-pm mailing list