2016-05-13 83 views
0

我在一個目錄中有多個文件,我想知道是否有任何util比較所有文件並輸出差異。或者有人可以幫我寫一個腳本來做到這一點? 編輯: 我有五個文件有一些值。我需要知道每個文件中的唯一值並將它們輸出到另一個文件中。在unix中的同一目錄中的多個文件比較

Sample1.txt 
001,20160512 
002,20160512 
003,20160512 

Sample2.txt 
001,20160512 
004,20160512 
006,20160512 

Sample3.txt 
004,20160512 
008,20160512 
007,20160512 

Sample4.txt 
008,20160512 
005,20160512 
006,20160512 

我的輸出應該比較兩個文件,比如Sample1.txt和Sample2.txt,並輸出唯一值。對於前:

Out1.txt 
Unique in Sample1.txt 
002,20160512 
003,20160512 

Out2.txt 
Unique in Sample2.txt 
004,20160512 
006,20160512 

等都在另一個比較Sample2.txt和Sample3.txt和輸出值出的文件和比較樣本3和Sample4,樣本1和樣本3,樣本1和Sample4,樣品2和Sample4並生成輸出在具有標題的不同文件中。

我不想使用vimdiff,因爲可能有四個以上的文件。

+1

你是什麼意思'比較所有文件? – GMichael

+0

@Michael我編輯了我的問題。 –

+0

使用'sort','uniq -c'和'grep'。我可以嘗試,如果你給真正的輸入行和期望的輸出 – GMichael

回答

1

使用bash陣列和join存儲的文件列表和循環我試圖讓文件中的唯一所有的概率

#!/bin/bash 

# List of files, can be modified as needed, can be any number of files 
# The logic will work even if the files have a .txt extension, but 
# the final output file names will look odd 

filelist=(file1 file2 file3 file4) 

# 'for' loop logic added to get the unique entries in each of the following combinations and in each of the files 

# file1 file2 
# file1 file3 
# file1 file4 
# file2 file3 
# file2 file4 
# file3 file4 

# Outer for loop 
for ((i=0; i<${#filelist[@]} ; i+=1)) ; do 
    # Inner for loop 
    for ((j=i+1; j<${#filelist[@]} ; j+=1)) ; do 

    echo "Unique between ${filelist[i]}" "${filelist[j]}" > unique${filelist[i]}${filelist[j]}.txt 

    echo -e "Unique in ${filelist[i]}" >> unique${filelist[i]}${filelist[j]}.txt 

    # Will produce unique lines in 'file i' when comparing 'file i' and 'file j' 
    join -v 1 <(sort ${filelist[i]}) <(sort ${filelist[j]}) >> unique${filelist[i]}${filelist[j]}.txt 

    echo -e "Unique in ${filelist[j]}" >> unique${filelist[i]}${filelist[j]}.txt 

    # Will produce unique lines in 'file j' when comparing 'file i' and 'file j' 
    join -v 2 <(sort ${filelist[i]}) <(sort ${filelist[j]}) >> unique${filelist[i]}${filelist[j]}.txt 

    done 

done 

將輸出文件如下

$ ls unique* 
uniquefile1file2.txt uniquefile1file3.txt uniquefile1file4.txt uniquefile2file3.txt uniquefile2file4.txt uniquefile3file4.txt 

而在每個文件內容如下

$ cat uniquefile1file2.txt 
Unique between file1 file2 
Unique in file1 
002,20160512 
003,20160512 
Unique in file2 
004,20160512 
006,20160512 
+0

這工作正是我想要的。 –

1

您可以使用下面的提示:

diff --suppress-common-lines Sample1.txt Sample2.txt | awk 'BEGIN {print "Unique in Sample1.txt";} /</{print $2;}'