bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: wc -l is not working the way it should


From: Bob Proulx
Subject: Re: wc -l is not working the way it should
Date: Wed, 16 Jan 2008 17:10:34 -0700
User-agent: Mutt/1.5.13 (2006-08-11)

Rahman, Syed A  wrote:
> The wc seems not working correctly, say for example
> wc -l filename | cut -c1-8 
> should only give the lines, but this your version of wc, behaves
> differently. for small size files it does not reserve first 8 colums for
> size so get the file name as well

Thank you for the report.  However this is not a bug in wc.  The wc
command is behaving correctly and according to standards when it
produces this output.

This is one of the long standing differences between SysV and BSD
types of systems.  SysV wc produces output without leading spaces.
BSD wc produces output with leading spaces.

> All others give this
> $ wc -l test.ksh | cut -c1-8
>       14

You must not have tested any SysV systems.  The HP-UX wc is one
example of a system that does not produce leading spaces.

> Your version gives this----
> $ wc -l test.ksh | cut -c1-8
> 14 test.

This is intended behavior.  The NEWS for wc 5.0.90 (2003-07-29) lists:

  wc count field widths now are heuristically adjusted depending on the input
  size, if known.  If only one count is printed, it is guaranteed to
  be printed without leading spaces.

  Previously, wc did not align the count fields if POSIXLY_CORRECT was set,
  but POSIX did not actually require this undesirable behavior, so it
  has been removed.

Portable scripts should not assume anything about leading spaces here
and should trim them as required.  Instead of using cut to remove the
filename for this it would be better simply to apply the file to stdin
such that the filename is not output.

  $ wc -l < test.ksh
  14
  $ lines=$(wc -l < test.ksh)
  $ echo "lines=$lines"
  lines=14

Then to additionally handle systems which print leading spaces one
possible technique is to allow the shell's IFS to discard the extra
spaces.  Note that 'echo' is a shell builtin and does not use an extra
process.

  $ wc -l < test.ksh
       14
  $ lines=$(echo $(wc -l < test.ksh) )
  $ echo "lines=$lines"
  lines=14

Bob




reply via email to

[Prev in Thread] Current Thread [Next in Thread]