zsh-users
 help / color / mirror / code / Atom feed
From: "Benjamin R. Haskell" <zsh@benizi.com>
To: TJ Luoma <luomat@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: wc and leading spaces
Date: Mon, 22 Aug 2011 21:37:45 -0400 (EDT)	[thread overview]
Message-ID: <alpine.LNX.2.01.1108222126130.10525@hp.internal> (raw)
In-Reply-To: <CADjGqHvYVmvqV+mF9Cdk6wbPoeq-n0pgSJQHwGm1xjaG1T_XVg@mail.gmail.com>

[-- 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

  parent reply	other threads:[~2011-08-23  1:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-22 23:53 TJ Luoma
2011-08-23  0:21 ` Vincent Lefevre
2011-08-23  0:24 ` Vincent Lefevre
2011-08-23  1:37 ` Benjamin R. Haskell [this message]
2011-08-23  2:31 ` Aaron Davies

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LNX.2.01.1108222126130.10525@hp.internal \
    --to=zsh@benizi.com \
    --cc=luomat@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).