[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#24532: GNU wc --lines doesn't report last line when that doesn't end
From: |
Carlo Wood |
Subject: |
bug#24532: GNU wc --lines doesn't report last line when that doesn't end on a new-line. |
Date: |
Sun, 25 Sep 2016 02:08:12 +0200 |
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<no new-line>
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<no new-line>
... 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 <address@hidden>
- bug#24532: GNU wc --lines doesn't report last line when that doesn't end on a new-line.,
Carlo Wood <=