2016-04-24 37 views
0

根據我的2列表,可以使用哪些unix命令/實用程序創建最終名稱列表?這就像是在做一個SET的操作。但是,主要的比較必須是針對NameList1。如果NameList2有一個額外的名稱,請不要將它包含在主列表中。unix命令執行與設置操作相當的列表比較

注:我不能讓它排序(按字母順序排列等)。我需要保留列位置(自然順序),正如我在列表頂部提供的那樣。謝謝你的幫助。

NameList1:

Joe 
John 
Mary 
Mike 
Allan 
David 
Andrew 
Matt 

NameList2:

Joe 
John 
Mary 
George 
Jeff 
Allan 
David 
Andrew 
Frank 

如果我做一個diff一個並排側比較就可以看到比較。像這樣:

-bash-4.1$ diff --side-by-side NameList1.txt NameList2.txt 
Joe        Joe 
John        John 
Mary        Mary 
Mike         | George 
            > Jeff 
Allan        Allan 
David        David 
Andrew        Andrew 
Matt         | Frank 

結果,預期爲我的最終名單將是:

Joe 
John 
Mary 
Mike 
Allan 
David 
Andrew 
Matt 

George, Jeff, Frank從NameList1刪除。我怎樣才能產生這個最終名單?有更好的工具命令嗎?我正確使用diff嗎?

+0

是與您的list1相同的結果嗎? – haifzhan

+0

您的問題沒有很好地定義。 –

+0

正如我的'主'列表中列出的是我需要生產的。所以換句話說,無論NameList2中哪些不在NameList1中,都將其從NameList1中移除。 – noober

回答

1

這可以使用簡單的join命令來實現。 在我的Mac,以下給出了預期的結果

join -a1 NameList1 NameList2

>  -a file_number 
>     In addition to the default output, produce a line for each 
>     unpairable line in file file_number. 

編輯

在Linux上,--no-checkorder選項將避免檢查對輸入的排序順序。

join -a1 --nocheck-order NameList1 NameList2

+0

這個工作,但是有沒有辦法抑制輸出'join:file 2不按排序順序','連接:file1不是按排序順序'? – noober

+0

我在Mac中沒有收到此消息。可能會有轉變來抑制警告?看看'男人加入' –

+0

啊..你是正確的在它的作品。然而,我正在運行centOS 6,並且我沒有在手冊頁上看到要抑制這一點。 – noober