How to detect file ends in newline?

半城伤御伤魂 提交于 2019-12-29 05:49:05

问题


Over at Can you modify text files when committing to subversion? Grant suggested that I block commits instead.

However I don't know how to check a file ends with a newline. How can you detect that the file ends with a newline?


回答1:


@Konrad: tail does not return an empty line. I made a file that has some text that doesn't end in newline and a file that does. Here is the output from tail:

$ cat test_no_newline.txt
this file doesn't end in newline$ 

$ cat test_with_newline.txt
this file ends in newline
$

Though I found that tail has get last byte option. So I modified your script to:

#!/bin/sh
c=`tail -c 1 $1`
if [ "$c" != "" ]; then echo "no newline"; fi



回答2:


Or even simpler:

#!/bin/sh
test "$(tail -c 1 "$1")" && echo "no newline at eof: '$1'"

But if you want a more robust check:

test "$(tail -c 1 "$1" | wc -l)" -eq 0 && echo "no newline at eof: '$1'"



回答3:


Here is a useful bash function:

function file_ends_with_newline() {
    [[ $(tail -c1 "$1" | wc -l) -gt 0 ]]
}

You can use it like:

if ! file_ends_with_newline myfile.txt
then
    echo "" >> myfile.txt
fi
# continue with other stuff that assumes myfile.txt ends with a newline



回答4:


You could use something like this as your pre-commit script:

#! /usr/bin/perl

while (<>) {
    $last = $_;
}

if (! ($last =~ m/\n$/)) {
    print STDERR "File doesn't end with \\n!\n";
    exit 1;
}



回答5:


Worked for me:

tail -n 1 /path/to/newline_at_end.txt | wc --lines
# according to "man wc" : --lines - print the newline counts

So wc counts number of newline chars, which is good in our case. The oneliner prints either 0 or 1 according to presence of newline at the end of the file.




回答6:


Using only bash:

x=`tail -n 1 your_textfile`
if [ "$x" == "" ]; then echo "empty line"; fi

(Take care to copy the whitespaces correctly!)

@grom:

tail does not return an empty line

Damn. My test file didn't end on \n but on \n\n. Apparently vim can't create files that don't end on \n (?). Anyway, as long as the “get last byte” option works, all's well.



来源:https://stackoverflow.com/questions/38746/how-to-detect-file-ends-in-newline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!