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

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

提交回复
热议问题