Hi,

I wrote a ruby code that writes to a file:

open('file.txt', 'w') {|file| file.write(arr.join("\n")) }

when I try to get the number of line counts

wc -l file.txt

I get the count as 49 instead of 50. How to overcome this? My file might not have a new line at the end. That shouldn't have prevented wc to count the number of actual lines.

wc - word count with the -l option count the number of newlines

This is right from man wc

-l'
`--lines'
Print only the newline counts.

Most utilities work with text files. A text file, by definition, has a newline at the end. If there's no final newline, the last line will not be counted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.