On Tue, 3 Nov 2015 21:01:36 -0800 Philip Guenther <guent...@gmail.com> wrote: > On Tue, Nov 3, 2015 at 8:35 PM, dan mclaughlin <thev...@openmailbox.org> wrote > > i was writing a script when i ran across this. > > > > $ echo "hello world" | wc > > 1 2 12 > > $ echo -n "hello world" | wc > > 0 2 11 > > > > the following patch corrects this: > > ...but it's not a bug. wc's line count is required by POSIX to be a > count of the number of newline characters in the involved input. > > > Philip Guenther >
i had a suspicion that that was a possibility, but it didn't make sense to me. can't imagine that the last line wouldn't be a line... out of curiosity, do you know why? some historic artifact? either way, maybe a slight modification to the manpage is in order: --- usr.bin/wc/wc.1.orig Sat May 2 05:10:42 2015 +++ usr.bin/wc/wc.1 Wed Nov 4 00:48:01 2015 @@ -69,7 +69,7 @@ Use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Petabyte, and Exabyte in order to reduce the number of digits to four or fewer using powers of 2 for sizes (K=1024, M=1048576, etc.). .It Fl l -The number of lines in each input file +The number of newlines in each input file is written to the standard output. .It Fl m Intended to count characters instead of bytes; since other utilities (like tail, grep) will count that as a line (and i just have trouble wrapping my head around the idea that it's not a line.) i could write a patch adding an argument for non-POSIX mode if that might be interesting to anyone. On Tue, 03 Nov 2015 22:08:34 -0700 "Anthony J. Bentley" <anth...@anjbe.name> wrote: > dan mclaughlin writes: > > i was writing a script when i ran across this. > > Is there a wc that doesn't behave this way? POSIX wc explicitly counts > newline characters. So this is probably the behavior people write their > scripts against. > guess i'll have to expect that behaviour too. must've been lucky not to have run into a problem before, especially as i'll use output from 'wc -l' for things like tail and sed. time to do some auditing of my scripts... also wondering how to get equivalent functionality. maybe there's a better way, but off the top of my head i can only think of: grep -n .* | tail -1 | sed 's/:.*//'