質問

以上で ですが変更テキストファイルの場合におけるsubversion? 助成 示唆されたブロックを犯します。

しかしないわからないかチェックファイルの最後の改行.どのようにすることができますこのファイルの終わり改行?

役に立ちましたか?

解決

@Konrad:テール戻らない空の路線です。作ったファイルはテキストなの末尾の改行およびファイルとする。ここでは、出力からテール:

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

$ cat test_with_newline.txt
this file ends in newline
$

もうテールしてく最後のバイトのオプションです。う変更しスクリプト:

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

他のヒント

またはより簡単に:

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

たい場合にはより強固なチェック:

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

こちらはbashの機能:

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

使用できるように:

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

を使用できるようなこととして事前にコミットスクリプト:

#! /usr/bin/perl

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

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

詳細:

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

でwcカウント数での改行文字数をいいます。のoneliner版画は0,1のどちらでもかに存在感の改行末でのファイルです。

のみを使用 bash:

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

(注意の空白文字を正しく!)

@grom:

テール戻らない空の線

気.私のテストファイルな終了 \n\n\n.どうやら vim できない創出ていないファイル終了 \n (?).いずれにせよ、の取得"last byte"のオプションの作品について書かれているものです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top