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

后端 未结 12 1268
广开言路
广开言路 2021-02-01 03:45

How can I list normal text (.txt) filenames, that don\'t end with a newline?

e.g.: list (output) this filename:

$ cat a.txt
asdfasdlsad4rand         


        
12条回答
  •  遇见更好的自我
    2021-02-01 04:04

    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$' .
    

    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$')

    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

提交回复
热议问题