たとえば、
ファイル1.txt:
I need to buy apples.
I need to run the laundry.
I need to wash the dog.
I need to get the car detailed.
ファイル2.txt
I need to buy apples.
I need to run the laundry.
I need to wash the car.
I need to get the car detailed.
ファイル3.txt
I need to wash the car.
これにより、diff file1.txt file2.txt
diffコマンドは、file2.txtにある場合はfile3.txtのステートメントを無視する必要があります。したがって、この場合は違いはありません。
無視フラグ(diff -I "$line"
)を使用すると、両方のファイルでパターンを見つけることができるので役に立ちません。
どうすればいいですか?
ベストアンサー1
回避策は、その行を削除して比較することです。つまり、どちらも次のようにfile1
なりますfile2
。
I need to buy apples.
I need to run the laundry.
I need to get the car detailed.
grep
perl
、および次の組み合わせでこれを行うことができますsed
。
$ lines_to_ignore=$(grep -nFf file3 file2 | perl -pe 's|^(\d+):.*|$1s/.//g;|')
$ echo $lines_to_ignore
3s/.//g;
$ diff <(sed "$lines_to_ignore" file1) <(sed "$lines_to_ignore" file2)
$ echo $?
0
- 私は
grep
(行番号付き)一致する行を取得するために使用しますfile2
perl
その後、出力から行番号を取得してgrep
sedコマンドを生成しました(Ns/.//g
N行のすべての文字を削除)。sed
次に、プロセス置換を使用してファイルに対してこれらのコマンドを実行した結果をdiff
。