From unknown Sun Jun 15 08:44:33 2025 X-Loop: help-debbugs@gnu.org Subject: bug#24532: GNU wc --lines doesn't report last line when that doesn't end on a new-line. Resent-From: Carlo Wood Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Sun, 25 Sep 2016 00:12:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 24532 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: To: 24532@debbugs.gnu.org X-Debbugs-Original-To: bug-coreutils@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.14747622933525 (code B ref -1); Sun, 25 Sep 2016 00:12:03 +0000 Received: (at submit) by debbugs.gnu.org; 25 Sep 2016 00:11:33 +0000 Received: from localhost ([127.0.0.1]:34907 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bnx2b-0000ui-HK for submit@debbugs.gnu.org; Sat, 24 Sep 2016 20:11:33 -0400 Received: from eggs.gnu.org ([208.118.235.92]:35756) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bnwzl-0000q2-7K for submit@debbugs.gnu.org; Sat, 24 Sep 2016 20:08:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bnwze-0002v6-M6 for submit@debbugs.gnu.org; Sat, 24 Sep 2016 20:08:31 -0400 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50 autolearn=disabled version=3.3.2 Received: from lists.gnu.org ([2001:4830:134:3::11]:54175) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bnwze-0002ul-Il for submit@debbugs.gnu.org; Sat, 24 Sep 2016 20:08:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54958) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bnwzc-0005qP-9s for bug-coreutils@gnu.org; Sat, 24 Sep 2016 20:08:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bnwzX-0002rv-81 for bug-coreutils@gnu.org; Sat, 24 Sep 2016 20:08:28 -0400 Received: from smtpq2.tb.mail.iss.as9143.net ([212.54.42.165]:39413) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bnwzX-0002oa-0y for bug-coreutils@gnu.org; Sat, 24 Sep 2016 20:08:23 -0400 Received: from [212.54.42.117] (helo=lsmtp3.tb.mail.iss.as9143.net) by smtpq2.tb.mail.iss.as9143.net with esmtp (Exim 4.82) (envelope-from ) id 1bnwzN-0003kY-8T for bug-coreutils@gnu.org; Sun, 25 Sep 2016 02:08:13 +0200 Received: from h172167.upc-h.chello.nl ([62.194.172.167] helo=mail9.alinoe.com) by lsmtp3.tb.mail.iss.as9143.net with esmtp (Exim 4.82) (envelope-from ) id 1bnwzN-0002Lf-6P for bug-coreutils@gnu.org; Sun, 25 Sep 2016 02:08:13 +0200 Received: from carlo by mail9.alinoe.com with local (Exim 4.80) (envelope-from ) id 1bnwzM-0000Zw-T9 for bug-coreutils@gnu.org; Sun, 25 Sep 2016 02:08:12 +0200 Date: Sun, 25 Sep 2016 02:08:12 +0200 From: Carlo Wood Message-ID: <20160925020812.239968e2@hikaru.localdomain> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-SA-Exim-Connect-IP: X-SA-Exim-Mail-From: carlo@alinoe.com X-SA-Exim-Scanned: No (on mail9.alinoe.com); SAEximRunCond expanded to false X-Ziggo-spambar: / X-Ziggo-spamscore: 0.0 X-Ziggo-spamreport: CMAE Analysis: v=2.1 cv=a+rNjhmF c=1 sm=0 tr=0 a=kj9zAlcOel0A:10 a=GW1xBdLrtEIA:10 a=BjFOTwK7AAAA:8 a=4-RufklTQAOD-DToeCEA:9 a=CjuIK1q_8ugA:10 a=N3Up1mgHhB-0MyeZKEz1:22 xcat=Undefined/Undefined none X-Ziggo-Spam-Status: No X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2001:4830:134:3::11 X-Spam-Score: -5.0 (-----) X-Mailman-Approved-At: Sat, 24 Sep 2016 20:11:31 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -5.0 (-----) You can argue that this is a feature, but I consider it a bug for all practical purposes. A text file might be REQUIRED to end on a EOL sequence (ie, '\n' for linux), in which case wc --lines works, but consider for a moment a (otherwise) text file where the last line does not end on a new-line: echo "Line 1" > testfile echo "Line 2" >> testfile echo -n "Line 3" >> testfile Now try to print the last two lines as follows: $ tail -n 2 testfile Line 2 Line 3 Hence, tail considers it to be a line. Now suppose we want to print line 2 and all lines that follow, then a reasonable attempt would be (using bash scripting): total_lines=$(cat testfile | wc --lines) remaining_lines=$((total_lines - 1)) tail -n $remaining_lines testfile Right? Counter intuitive that results in: Line 3 ... Line 2 wasn't printed! Finally, sed is known to report the number of lines as follows: $ sed -n '$=' testfile 3 Hence, I consider it a bug that: $ wc --lines testfile 2 testfile Regards, -- Carlo Wood From unknown Sun Jun 15 08:44:33 2025 MIME-Version: 1.0 X-Mailer: MIME-tools 5.505 (Entity 5.505) X-Loop: help-debbugs@gnu.org From: help-debbugs@gnu.org (GNU bug Tracking System) To: Carlo Wood Subject: bug#24532: closed (Re: bug#24532: GNU wc --lines doesn't report last line when that doesn't end on a new-line.) Message-ID: References: <008e722b-ef57-7dcd-4895-64c38849269b@cs.ucla.edu> <20160925020812.239968e2@hikaru.localdomain> X-Gnu-PR-Message: they-closed 24532 X-Gnu-PR-Package: coreutils Reply-To: 24532@debbugs.gnu.org Date: Sun, 25 Sep 2016 02:01:01 +0000 Content-Type: multipart/mixed; boundary="----------=_1474768861-13856-1" This is a multi-part message in MIME format... ------------=_1474768861-13856-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Your bug report #24532: GNU wc --lines doesn't report last line when that doesn't end on a = new-line. which was filed against the coreutils package, has been closed. The explanation is attached below, along with your original report. If you require more details, please reply to 24532@debbugs.gnu.org. --=20 24532: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D24532 GNU Bug Tracking System Contact help-debbugs@gnu.org with problems ------------=_1474768861-13856-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at 24532-done) by debbugs.gnu.org; 25 Sep 2016 02:00:22 +0000 Received: from localhost ([127.0.0.1]:34930 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bnyju-0003aR-GH for submit@debbugs.gnu.org; Sat, 24 Sep 2016 22:00:22 -0400 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:33850) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bnyjs-0003a9-Mt for 24532-done@debbugs.gnu.org; Sat, 24 Sep 2016 22:00:21 -0400 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id DEF2416118E; Sat, 24 Sep 2016 19:00:13 -0700 (PDT) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id TgIc5rtXP51R; Sat, 24 Sep 2016 19:00:13 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 324F5161177; Sat, 24 Sep 2016 19:00:13 -0700 (PDT) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 2Izd9zYPlpm4; Sat, 24 Sep 2016 19:00:13 -0700 (PDT) Received: from [192.168.1.9] (unknown [47.153.191.53]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 0BB95160EA7; Sat, 24 Sep 2016 19:00:13 -0700 (PDT) Subject: Re: bug#24532: GNU wc --lines doesn't report last line when that doesn't end on a new-line. To: Carlo Wood , 24532-done@debbugs.gnu.org References: <20160925020812.239968e2@hikaru.localdomain> From: Paul Eggert Organization: UCLA Computer Science Department Message-ID: <008e722b-ef57-7dcd-4895-64c38849269b@cs.ucla.edu> Date: Sat, 24 Sep 2016 19:00:12 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 In-Reply-To: <20160925020812.239968e2@hikaru.localdomain> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Spam-Score: -3.1 (---) X-Debbugs-Envelope-To: 24532-done X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.1 (---) Carlo Wood wrote: > You can argue that this is a feature, but I consider it a bug for all > practical purposes. POSIX requires that wc -l must just count newlines, so it is indeed a fea= ture. If wc -l also counted incomplete lines at the end of a file, this would r= esult=20 in counterintuitive behavior of a different sort. For example: cat a b >c wc -l a wc -l b wc -l c Currently the first two numbers must sum to the third, but that would not= be=20 true under the change you're proposing. Incomplete lines must cause a problem of some sort, and I'm afraid that t= he=20 longstanding tradition is to cause the problem you ran into. ------------=_1474768861-13856-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at submit) by debbugs.gnu.org; 25 Sep 2016 00:11:33 +0000 Received: from localhost ([127.0.0.1]:34907 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bnx2b-0000ui-HK for submit@debbugs.gnu.org; Sat, 24 Sep 2016 20:11:33 -0400 Received: from eggs.gnu.org ([208.118.235.92]:35756) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bnwzl-0000q2-7K for submit@debbugs.gnu.org; Sat, 24 Sep 2016 20:08:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bnwze-0002v6-M6 for submit@debbugs.gnu.org; Sat, 24 Sep 2016 20:08:31 -0400 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50 autolearn=disabled version=3.3.2 Received: from lists.gnu.org ([2001:4830:134:3::11]:54175) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bnwze-0002ul-Il for submit@debbugs.gnu.org; Sat, 24 Sep 2016 20:08:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54958) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bnwzc-0005qP-9s for bug-coreutils@gnu.org; Sat, 24 Sep 2016 20:08:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bnwzX-0002rv-81 for bug-coreutils@gnu.org; Sat, 24 Sep 2016 20:08:28 -0400 Received: from smtpq2.tb.mail.iss.as9143.net ([212.54.42.165]:39413) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bnwzX-0002oa-0y for bug-coreutils@gnu.org; Sat, 24 Sep 2016 20:08:23 -0400 Received: from [212.54.42.117] (helo=lsmtp3.tb.mail.iss.as9143.net) by smtpq2.tb.mail.iss.as9143.net with esmtp (Exim 4.82) (envelope-from ) id 1bnwzN-0003kY-8T for bug-coreutils@gnu.org; Sun, 25 Sep 2016 02:08:13 +0200 Received: from h172167.upc-h.chello.nl ([62.194.172.167] helo=mail9.alinoe.com) by lsmtp3.tb.mail.iss.as9143.net with esmtp (Exim 4.82) (envelope-from ) id 1bnwzN-0002Lf-6P for bug-coreutils@gnu.org; Sun, 25 Sep 2016 02:08:13 +0200 Received: from carlo by mail9.alinoe.com with local (Exim 4.80) (envelope-from ) id 1bnwzM-0000Zw-T9 for bug-coreutils@gnu.org; Sun, 25 Sep 2016 02:08:12 +0200 Date: Sun, 25 Sep 2016 02:08:12 +0200 From: Carlo Wood To: bug-coreutils@gnu.org Subject: GNU wc --lines doesn't report last line when that doesn't end on a new-line. Message-ID: <20160925020812.239968e2@hikaru.localdomain> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-SA-Exim-Connect-IP: X-SA-Exim-Mail-From: carlo@alinoe.com X-SA-Exim-Scanned: No (on mail9.alinoe.com); SAEximRunCond expanded to false X-Ziggo-spambar: / X-Ziggo-spamscore: 0.0 X-Ziggo-spamreport: CMAE Analysis: v=2.1 cv=a+rNjhmF c=1 sm=0 tr=0 a=kj9zAlcOel0A:10 a=GW1xBdLrtEIA:10 a=BjFOTwK7AAAA:8 a=4-RufklTQAOD-DToeCEA:9 a=CjuIK1q_8ugA:10 a=N3Up1mgHhB-0MyeZKEz1:22 xcat=Undefined/Undefined none X-Ziggo-Spam-Status: No X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2001:4830:134:3::11 X-Spam-Score: -5.0 (-----) X-Debbugs-Envelope-To: submit X-Mailman-Approved-At: Sat, 24 Sep 2016 20:11:31 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -5.0 (-----) You can argue that this is a feature, but I consider it a bug for all practical purposes. A text file might be REQUIRED to end on a EOL sequence (ie, '\n' for linux), in which case wc --lines works, but consider for a moment a (otherwise) text file where the last line does not end on a new-line: echo "Line 1" > testfile echo "Line 2" >> testfile echo -n "Line 3" >> testfile Now try to print the last two lines as follows: $ tail -n 2 testfile Line 2 Line 3 Hence, tail considers it to be a line. Now suppose we want to print line 2 and all lines that follow, then a reasonable attempt would be (using bash scripting): total_lines=$(cat testfile | wc --lines) remaining_lines=$((total_lines - 1)) tail -n $remaining_lines testfile Right? Counter intuitive that results in: Line 3 ... Line 2 wasn't printed! Finally, sed is known to report the number of lines as follows: $ sed -n '$=' testfile 3 Hence, I consider it a bug that: $ wc --lines testfile 2 testfile Regards, -- Carlo Wood ------------=_1474768861-13856-1--