Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: wc and leading spaces



On Mon, 22 Aug 2011, TJ Luoma wrote:

Can anyone explain why 'wc' adds leading spaces to its output? ^1

for example:

$ ls | wc -l | sed 's# #~#g'
~~~~1299

or in another dir:

$ ls | wc -l | sed 's# #~#g'
~~~~~~47

I don't understand why:

a) anyone would want leading spaces

b) why they add enough spaces so that the numbers are "right" justified (that might not be the proper term, but you get the idea)

I don't get leading spaces² when any of '-c/--bytes', '-m/--chars', or '-l/--lines' is added. Without one of those options, the output of `wc` isn't useful for machine consumption without processing, so they made it "pretty" for human consumption.


Zsh question: Is there a way to get rid of the spaces without using either "| awk '{print $1}'" or "| sed 's#^ *##g'"?

var=${=$(ls | wc -l)}

A possibly better way to count files: (pretty sure that was just an example... but either way...):

set -- *(N)
print $#

A possibly better way to count lines (keeping with the ls example):

${#${(f):-"$(ls)"}}


^1 — well, GNU's 'wc' does not seem to add leading spaces, but my standard 'wc' in Mac OS X does…

²: with GNU `wc`... --version
wc (GNU coreutils) 8.12

--
Best,
Ben


Messages sorted by: Reverse Date, Date, Thread, Author