Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
×
User Journal

Journal eglamkowski's Journal: vi vs. wc 7

The info bar in my vi session says:
foo.csv 40000 lines --100%-- 40000,1 Bot

% wc foo.csv
    39999 160742 2521858 foo.csv

Why are these off by one? The last line of the file is NOT a blank line, it has just as much data on it as every other line. This isn't the first time I've noticed it, either. Why can't vi and wc agree on the number of lines in a file?

This discussion has been archived. No new comments can be posted.

vi vs. wc

Comments Filter:
  • Subject says it all. Could it be that vi is translating a DOS line ending for you?
    • The 'file' command does show it as ASCII text, and it is a csv, so probably exported from Excel, which would suggest it was originally a DOS file, but I am manipulating it on a unix system.

      When I drop the csv file into excel, it does show 40,000 lines, and in excel row 1 is number 1, not 0.

      Look at it in a hex editor, it appears the last record has no EOL marker of any sort - no new line or carriage return or anything. Between records there is a '0a' byte - new line. No new line after the last record.

      So, w
      • by lithron ( 88998 )
        A man on wc shows that wc only counts new lines.

        vi also shows "incomplete last line" when I open a file that doesn't have a trailing EOL character on the last line.

        So it looks like they do count things differently.
  • Is it whether they first line is counted as "1" or "0"? That'd be my pretty darn uninformed guess.
  • Is the last character a newline? If not, that might explain the difference.

    I doubt that that is it, though; wc being the more specialist tool is most likely right, IMO. You could perhaps write your own wc and see if it disagrees? This would seem to me to be the easiest way of adjudicating, short of manually counting the lines...

  • For the definitive answer, try Emacs.

It is contrary to reasoning to say that there is a vacuum or space in which there is absolutely nothing. -- Descartes

Working...