Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find files that do not end with a newline/linefeed?

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  $ 
like image 917
LanceBaynes Avatar asked Jan 07 '11 23:01

LanceBaynes


People also ask

How do I find line breaks in a text file?

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).

How do I know if a file is LF or CR LF?

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.

Is every file on New line?

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”.


Video Answer


2 Answers

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

like image 173
Anthony Bush Avatar answered Oct 05 '22 23:10

Anthony Bush


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"' 
like image 20
Julien Palard Avatar answered Oct 06 '22 01:10

Julien Palard