"wc - l foo.csv" returns 1 less than total number of lines

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

"wc - l foo.csv" returns 1 less than total number of lines

Post by Luke »

I am not really a linux expert by any means, but shouldn't this command return the number of lines in "foo.csv"?

Code: Select all

wc -l foo.csv
When I execute this, if the file is 4 lines long, it returns 3, if the file is 200 lines long, it returns 199. What is the deal?? :(
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: "wc - l foo.csv" returns 1 less than total number of lines

Post by VladSun »

It counts the number of new lines - so, if you have no new line char after the last char at the last line of your file it will ignore it.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: "wc - l foo.csv" returns 1 less than total number of lines

Post by Luke »

Oh, well that makes perfect sense. :) Guess I will just have to make sure the file ends in a newline then.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: "wc - l foo.csv" returns 1 less than total number of lines

Post by Chris Corbyn »

The Ninja Space Goat wrote:Oh, well that makes perfect sense. :) Guess I will just have to make sure the file ends in a newline then.
This is almost always good practise. But in any case would be easy to check :)
Post Reply