Thursday, 26 May 2011

unix WC command out by 1 - wtf

I am develop on a windows PC (for my sins) but to keep me feeling secure I use cygwin (a unix shell for windows. Today this has done me in.

The application I am working has to create files and in the final line of the file it has to include the number of the lines in the file. All simple enough and it did exactly what it was supposed to do.

However when I came to check the results manually I used the unix 'wc' command and this gave me a row count 1 less than what I was expecting and what the footer said was in the file. Strange.

I edited the file with 'vim' and low & behold wc was incorrect - what is going on ?

I then edited the file with hexEdit and noticed there was no CR & LF on the last line. Adding these made wc give the correct count.


Anonymous said...

I saw the same thing today. Were you able to figure out why? I copied the files with pscp from windows to unix

Bill Comer said...

Must admit, I did not look any furthur. Sorry.

Jeremy Stein said...

This is a mismatch between Unix and Windows. Windows does not require a final newline. Unix does. In Windows, if there is a final newline, it's treated as a blank line.