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

后端 未结 12 1246
广开言路
广开言路 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

    0 讨论(0)
  • 2021-02-01 04:05

    If you are using 'ack' (http://beyondgrep.com) as a alternative to grep, you just run this:

    ack -v '\n$'
    

    It actually searches all lines that don't match (-v) a newline at the end of the line.

    0 讨论(0)
  • 2021-02-01 04:08

    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"'
    
    0 讨论(0)
  • 2021-02-01 04:09

    Since your question has the perl tag, I'll post an answer which uses it:

    find . -type f -name '*.txt' -exec perl check.pl {} +
    

    where check.pl is the following:

    #!/bin/perl 
    
    use strict;
    use warnings;
    
    foreach (@ARGV) {
        open(FILE, $_);
    
        seek(FILE, -2, 2);
    
        my $c;
    
        read(FILE,$c,1);
        if ( $c ne "\n" ) {
            print "$_\n";
        }
        close(FILE);
    }
    

    This perl script just open, one per time, the files passed as parameters and read only the next-to-last character; if it is not a newline character, it just prints out the filename, else it does nothing.

    0 讨论(0)
  • 2021-02-01 04:10

    Give this a try:

    find . -type f -exec sh -c '[ -z "$(sed -n "\$p" "$1")" ]' _ {} \; -print
    

    It will print filenames of files that end with a blank line. To print files that don't end in a blank line change the -z to -n.

    0 讨论(0)
  • 2021-02-01 04:14

    If you have ripgrep installed:

    rg -l '[^\n]\z'
    

    That regular expression matches any character which is not a newline, and then the end of the file.

    0 讨论(0)
提交回复
热议问题