Subject: Re: bug#20954: wc - linux

From: Bob Proulx <[email-masked]>
Date: Sun, 5 Jul 2015 20:40:26 -0600

tele wrote:
> Maybe we did not understand.
> I don't want change old definitions but create new option for wc or echo,
> because this above examples not make logic sense,

What would such an option do?

> ( and it I want fix, however with sed is also fixed )

Your original message asked if "echo | wc -l" should count 0 lines
instead of 1 line.  But the echo is going to produce one line and
therefore it should be counted.

In a later message you wrote using sed to delete blank lines so that
only non-blank lines remained to be counted.

> $ a="" ; echo  "$a"  |  sed '/^\s*$/d' | wc -l
> 0
> 
> $ a="3" ; echo  "$a"  |  sed '/^\s*$/d' | wc -l
> 1
> 
> Can be added option to "wc" to fix this problem without use sed in future ?

This tells me that you as yet did not understand things yet. :-(

I tried to explain this in more detail in my response to that message.
The sed command you pulled from stackoverflow.com deletes blank
lines.  That is a good way to avoid counting blank lines.

If I guess at what you are suggesting then it does not make sense to
add an option to wc to count only non-blank lines.  If you don't want
to count blank lines then delete them first.  There are an infinite
number of possible things to count.  There cannot be an infinite
number of options implemented.  And using sed to delete blank lines is
the Right Way To Do Things.

> however now Iunderstand that they work correctly in accordance with
> accepted principles.

Yes.

> > What is a text line? A text line by definition ends with a
> > newline. This has been standardized to prevent different
> > implementations from implementing it differently and creating
> > portability problems. Therefore all standards compliant
> > implementations must implement it in the same way to prevent
> > portability problems.
> 
> " wc -l " in most examples working correct,

"most"?  No.  "wc -l" is working correctly in all examples. :-)

> because it " echo "  give's " \n " and "wc -l" count correct.

Yes.

> I mentioned about "wc", because for me build option "wc -a" for "echo"  or
> "echo -m"
> this is not important.
> Maybe exist hope for example create option "m" to echo  , " echo -m "
> which not will from new line, but first line if variable is empty
> and from new line if is not empty  ?
> 
> example:
> 
> echo -m "" | wc -l
> 0
> 
> echo -m "e" | wc -l
> 1

The shell is a programming language.  If not infinite number then a
very large number of possibilities may be implemented by programming
them in the shell.  All such possibilities should not be coded into
specific options.  Instead if you have a specific need it should be
programmed.  Simply write the code that says explicitly what you want
to do.  There are millions of lines of code written for various tasks.
All of those millions of lines should not be turned into specific
options.  If you want to delete blank lines then simply delete blank
lines.

This entire discussion feels like an XY problem.  Here is a collection
of explanations of the XY problem.

  http://www.perlmonks.org/?node_id=542341

The [email-masked] mailing list is the right place to follow up but
if you wrote there and said what you were trying to do and asking how
to do it in the shell people would try to help you there.

Bob