2017-09-08 132 views
1

內容我有我需要比較他們,因爲缺少一個2項,其他擁有的內容的兩個文本文件,但我不知道它,因爲它們是長。我試過diffvimdiff,但沒有運氣。我的文件都是此格式的混亂秩序:比較的文本文件,忽略順序和格式

item1 item2 item3 
item8 item10 item6 
item32 item12 item7 

我怎樣才能挑選出的文本文件中的一個已但是卻忽略了格式和順序對方缺乏哪些項目?

+0

你寫**兩個文本文件**,公佈這些輸入文件和預期結果 – RomanPerekhrest

回答

0

Cyrus的例子是迄今爲止短,更重要的一點,但認爲我會練習一些(詳細)awk ING ...

的樣本數據:

$ cat file1 
     item2 item3 
item8 item10 item6 
item32 item12 item7 

$ cat file2 
item1 item2 item3 
item8    item6 
     item12 item7 

假設:

  • 雖然說明中說某些項目可能會從一個文件中丟失,但我會假設可能有兩個文件中缺少項目
  • 不會擔心排序(輸入或輸出)
  • 不會對指導如何顯示輸出我只是做我自己勝,包括顯示,該項目是從
  • 缺少的文件名

一種可能awk溶液:

$ cat text.awk 
BEGIN { RS="" } 

NR==FNR { afile=FILENAME ; for (i=1;i<=NF;i++) a[$i]=1 ; next } 
     { bfile=FILENAME ; for (i=1;i<=NF;i++) b[$i]=1  } 

END { 
    for (x in a) 
     { if (! b[x]) 
      { printf "missing from %s : %s\n",bfile,x } 
     } 
    for (x in b) 
     { if (! a[x]) 
      { printf "missing from %s : %s\n",afile,x } 
     } 
} 
  • RS="":集行分離器(RS)爲空字符串;這樣將一個文件分割成一個長記錄
  • NR==NFR:如果這是第(二)文件...
  • afile=FILENAME:保存文件名以後打印
  • for/a[$i]=1:使用輸入字段1-NF作爲指標關聯數組a,數組值設置爲1(又名「真」)
  • next:讀下一記錄,在這種情況下,意味着讀取下一個文件
  • NR!=FNR:如果這是第二(兩個)文件...
  • s火焰處理,除了填入bfile和關聯數組b
  • END ...:處理我們的陣列...
  • for (x in a):通過數組a的指標循環,並分配給變量x,如果有數組b! b[x])沒有類似的索引條目,然後打印有關數組索引消息(實際上的名字從原來的文件項)從失蹤bfile
  • for (x in b):同以前的循環檢查,除了在bfile項目,但不是在afile

awk在動作腳本:

$ awk -f text.awk file1 file2 
missing from file2 : item10 
missing from file2 : item32 
missing from file1 : item1 

# switch the order of the input files => same messages, just different order 
$ awk -f text.awk file2 file1 
missing from file1 : item1 
missing from file2 : item10 
missing from file2 : item32 
0

我相信你可以使用通訊命令..但你應該在有序這兩個文件進行比較:

comm -23 f1 f2 # will give whatever lines not matching in file1 against file2 
comm -12 f1 f2 # will give matching lines 
comm -13 f1 f2 # will give whatever lines not matching in file2 against file 1 
+1

感謝Mr.batMan。 :)我已糾正它。 – VIRA

0

使用comm到您的文件進行比較,以找出最常見的還是在他們不同。

$ cat file1 
item1 item2 item3 
item8 item10 item6 
item32 item12 item5 

$ cat file2 
item1 item2 item3 
item8 item15 item6 
item32 item12 item7 

comm -23 file1 file2返回線,是file1中而不是在文件2條
comm -13 file1 file2返回線,是file2中而不是在文件1條
comm -12 file1 file2返回線,在這兩個文件共同

comm需要輸入文件到排序。我們將首先通過sed將spaces轉換爲\n,然後通過排序進行排序。

$ comm -23 <(sed 's/ \+/\n/g' file1 | sort) <(sed 's/ \+/\n/g' file2 | sort) 
item10 
item5 

$ comm -13 <(sed 's/ \+/\n/g' file1 | sort) <(sed 's/ \+/\n/g' file2 | sort) 
item15 
item7 

$ comm -12 <(sed 's/ \+/\n/g' file1 | sort) <(sed 's/ \+/\n/g' file2 | sort) 
item1 
item12 
item2 
item3 
item32 
item6 
item8 

- 我的答案在這裏結束。 ---

但只爲信息,通訊的手冊頁說:

With no options, comm produce three-column output. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files. 

    -1  suppress column 1 (lines unique to FILE1) 

    -2  suppress column 2 (lines unique to FILE2) 

    -3  suppress column 3 (lines that appear in both files) 

因此:

$ comm <(sed 's/ \+/\n/g' file1 | sort) <(sed 's/ \+/\n/g' file2 | sort) 
       item1 
item10 
       item12 
     item15 
       item2 
       item3 
       item32 
item5 
       item6 
     item7 
       item8