Print all complete lines of file

filesgrepnewlines

How do I print all complete lines of a file? By 'complete' I mean only the ones that end with newline character. grep treats EOF as line delimiter, so grep '^.*$' file will print the last line even if there's no newline at the end of file.

The whole problem comes from parsing log files: we need to somehow be sure that the last entry was completely logged – i.e. it ends with newline.

Best Answer

A simple approach is to use perl:

perl -ne '/\n/ && print' file

If you just want to check that the last character of a file is a newline, you can do:

tail -c1 file | grep -q '^$' && echo yes || echo no

But the -c option is not POSIX, so it isn't portable.

Related Question