zsh-users
 help / color / mirror / code / Atom feed
* wc and leading spaces
@ 2011-08-22 23:53 TJ Luoma
  2011-08-23  0:21 ` Vincent Lefevre
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: TJ Luoma @ 2011-08-22 23:53 UTC (permalink / raw)
  To: Zsh Users

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)


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

TjL

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: wc and leading spaces
  2011-08-22 23:53 wc and leading spaces TJ Luoma
@ 2011-08-23  0:21 ` Vincent Lefevre
  2011-08-23  0:24 ` Vincent Lefevre
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Vincent Lefevre @ 2011-08-23  0:21 UTC (permalink / raw)
  To: zsh-users

On 2011-08-22 19:53:38 -0400, TJ Luoma wrote:
> Can anyone explain why 'wc' adds leading spaces to its output? ^1

There are different implementations (including historical).
But a POSIX implementation mustn't add spaces. See:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/wc.html

(which also mentions the System V wc).

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: wc and leading spaces
  2011-08-22 23:53 wc and leading spaces TJ Luoma
  2011-08-23  0:21 ` Vincent Lefevre
@ 2011-08-23  0:24 ` Vincent Lefevre
  2011-08-23  1:37 ` Benjamin R. Haskell
  2011-08-23  2:31 ` Aaron Davies
  3 siblings, 0 replies; 5+ messages in thread
From: Vincent Lefevre @ 2011-08-23  0:24 UTC (permalink / raw)
  To: zsh-users

On 2011-08-22 19:53:38 -0400, TJ Luoma wrote:
> ^1 — well, GNU's 'wc' does not seem to add leading spaces, but my
> standard 'wc' in Mac OS X does…

GNU wc adds spaces when there are no options:

$ echo | wc
      1       0       1

Anyway, this is not related to zsh.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: wc and leading spaces
  2011-08-22 23:53 wc and leading spaces TJ Luoma
  2011-08-23  0:21 ` Vincent Lefevre
  2011-08-23  0:24 ` Vincent Lefevre
@ 2011-08-23  1:37 ` Benjamin R. Haskell
  2011-08-23  2:31 ` Aaron Davies
  3 siblings, 0 replies; 5+ messages in thread
From: Benjamin R. Haskell @ 2011-08-23  1:37 UTC (permalink / raw)
  To: TJ Luoma; +Cc: Zsh Users

[-- Attachment #1: Type: TEXT/PLAIN, Size: 1234 bytes --]

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: wc and leading spaces
  2011-08-22 23:53 wc and leading spaces TJ Luoma
                   ` (2 preceding siblings ...)
  2011-08-23  1:37 ` Benjamin R. Haskell
@ 2011-08-23  2:31 ` Aaron Davies
  3 siblings, 0 replies; 5+ messages in thread
From: Aaron Davies @ 2011-08-23  2:31 UTC (permalink / raw)
  To: Zsh Users

On Mon, Aug 22, 2011 at 7:53 PM, TJ Luoma <luomat@gmail.com> wrote:

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

one cheap way is to let zsh trim them off through word splitting rules

pro% wc foo
     175    1898   15898 foo
pro% echo $(wc foo)
175 1898 15898 foo

there's presumably something you can do with zsh pattern matching
(14.3 Parameter Expansion in the info manual)....
--
Aaron Davies
aaron.davies@gmail.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-08-23  2:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-22 23:53 wc and leading spaces TJ Luoma
2011-08-23  0:21 ` Vincent Lefevre
2011-08-23  0:24 ` Vincent Lefevre
2011-08-23  1:37 ` Benjamin R. Haskell
2011-08-23  2:31 ` Aaron Davies

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).