How can I list normal text (.txt
) filenames, that don't end with a newline?
e.g.: list (output) this filename:
$ cat a.txt asdfasdlsad4randomcharsf asdfasdfaasdf43randomcharssdf $
and don't list (output) this filename:
$ cat b.txt asdfasdlsad4randomcharsf asdfasdfaasdf43randomcharssdf $
Open any text file and click on the pilcrow (¶) button. Notepad++ will show all of the characters with newline characters in either the CR and LF format. If it is a Windows EOL encoded file, the newline characters of CR LF will appear (\r\n). If the file is UNIX or Mac EOL encoded, then it will only show LF (\n).
use a text editor like notepad++ that can help you with understanding the line ends. It will show you the line end formats used as either Unix(LF) or Macintosh(CR) or Windows(CR LF) on the task bar of the tool. you can also go to View->Show Symbol->Show End Of Line to display the line ends as LF/ CR LF/CR.
So, it turns out that, according to POSIX, every text file (including Ruby and JavaScript source files) should end with a \n , or “newline” (not “a new line”) character. This acts as the eol , or the “end of line” character. It is a line “terminator”.
Use pcregrep, a Perl Compatible Regular Expressions version of grep which supports a multiline mode using -M flag that can be used to match (or not match) if the last line had a newline:
pcregrep -LMr '\n\Z' .
In the above example we are saying to search recursively (-r
) in current directory (.
) listing files that don't match (-L
) our multiline (-M
) regex that looks for a newline at the end of a file ('\n\Z'
)
Changing -L
to -l
would list the files that do have newlines in them.
pcregrep
can be installed on MacOS with the homebrew pcre
package: brew install pcre
Ok it's my turn, I give it a try:
find . -type f -print0 | xargs -0 -L1 bash -c 'test "$(tail -c 1 "$0")" && echo "No new line at end of $0"'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With