Search code examples
bashwc

Real bash line count


I'm parsing a temp file whose lines would like removed so I run: tr '\n' ' ' < temp > temp2. Now when I wc -l temp2 it is returning 0 lines instead of 1 which was unexpected for me.

After checking the manual, wc -l counts just the newlines and not the lines. Its behaviour is fine but might be problematic if you don't know if the last line of a file contains a line feed.

Is there any tool or workaround that count lines even if they have no linefeed?


Solution

  • awk to the rescue!

    awk 'END{print NR}' file
    

    will work fine without the trailing newline.