2015-08-19 84 views
0

文件#1示例行:比較兩個文件,並只顯示有部分匹配

one two three 
four five six 
seven eight nine 
eleven 

文件#2例如:

two 
five 
nine.not 
eleven 

我想找個上的文件#任何線1包含文件#2中存在的任何字,示例輸出:

one two three 
four five six 
eleven 

我試圖查看是否有一種方法可以在linux命令行中完成,但尚未成功。有任何想法嗎?

感謝

回答

3

你可以試試:

grep -f file2 file1 

-f選項從文件2獲取模式(每行一個)

編輯

@Barmar評論

grep -F -w -f file2 file1 

與-w選項,像eleveneleven行不選擇

+3

使用'進行精確匹配,而不是正則表達式-F'選項,'-w'以匹配整個單詞。 – Barmar

1

您可以使用此awk命令:

awk 'FNR==NR{a[$1]; next} {for (i in a) if (index($0, i)) print}' file2 file1 
one two three 
four five six 
eleven 
相關問題