
私は5つの異なる場所から情報を取得し、その違いを比較するスクリプトを作成しようとしています。情報はIPアドレスにすぎず、それをテキストファイルに入れました。私は以下を使用しています:
diff --from-file file1 file2 file3 file4 file5
比較するとうまくいきますが、どのファイルに他の情報が含まれているかを示す必要があります。一致しないファイルは最大1つまたは最大2つであると予想されます。
答え1
-u
diff形式を均一またはに変更すると、--unified
ファイル名が表示されます。
# diff -u --from-file file1 file[2-5]
--- file1 2020-10-30 11:02:22.223269990 +0200
+++ file3 2020-10-30 11:02:35.445984702 +0200
@@ -1 +1 @@
-original
+new
--- file1 2020-10-30 11:02:22.223269990 +0200
+++ file5 2020-10-30 11:02:40.625872942 +0200
@@ -1 +1 @@
-original
+new
-q
またはで短い出力を使用することもできます--brief
。
# diff -q --from-file file1 file[2-5]
Files file1 and file3 differ
Files file1 and file5 differ
別の解決策は、すべてのファイルに対してmd5sum、sha1sumなどのチェックサムプログラムを実行し、どのファイルが最初のファイルと異なるチェックサムを持っているかを確認することです。
GNUシステムでは、次のものと組み合わせることもできます。アッこのように:
# md5sum file* | awk '{h[$1] = h[$1] " " $2} END {for(k in h) printf("%s:%s\n", k, h[k])}'
88fa9f694690e11239096536ccf2702b: file1 file2 file4
9cd599a3523898e6a12e13ec787da50a: file3 file5
または、次のように組み合わせることもできます。ユニークこのように:
# hashlen=32 # MD5 outputs 32 hexadecimals
# md5sum file* | sort | uniq --group --check-chars=${hashlen}
88fa9f694690e11239096536ccf2702b file1
88fa9f694690e11239096536ccf2702b file2
88fa9f694690e11239096536ccf2702b file4
9cd599a3523898e6a12e13ec787da50a file3
9cd599a3523898e6a12e13ec787da50a file5
FreeBSDシステムでは、以下を組み合わせることができます。アッこのように:
# md5 file* | awk -F ' = ' '{h[$2] = h[$2] " " substr($1, index($1, "("))} END {for(k in h) printf("%s:%s\n", k, h[k])}'
9cd599a3523898e6a12e13ec787da50a: (file3) (file5)
88fa9f694690e11239096536ccf2702b: (file1) (file2) (file4)
答え2
広がる5つの入力を処理できます(sudo apt-get install diff)。マニュアルページから:
Diffuse is a graphical tool for merging and comparing text files. Diffuse is able
to compare an arbitrary number of files side-by-side and gives users the ability
to manually adjust line matching and directly edit files.
答え3
ファイルが1つだけ異なる場合は、次のものを使用できます。
md5sum file* | uniq -f 2
uniq -f 2
フィールド2(ファイル名)は使用されません。