Saturday, February 19, 2022

[SOLVED] wc -l is NOT counting last of the file if it does not have end of line character

Issue

I need to count all lines of an unix file. The file has 3 lines but wc -l gives only 2 count.

I understand that it is not counting last line because it does not have end of line character

Could any one please tell me how to count that line as well ?


Solution

It is better to have all lines ending with EOL \n in Unix files. You can do:

{ cat file; echo ''; } | wc -l

Or this awk:

awk 'END{print NR}' file


Answered By - anubhava
Answer Checked By - Senaida (WPSolving Volunteer)