標(biāo)題: comm, an easy and powerful command [打印本頁] 作者: icesummit 時(shí)間: 2005-12-15 23:24 標(biāo)題: comm, an easy and powerful command In our work, we often encounter the following questions:
I have two files: file1 and file2:
1) How can I print out the lines that are only contained in file1?
2) How can I print out the lines that are only contained in file2?
3) How can I print out the lines that are contained both in file1 and file2?
There is a powerful shell command that can easily meet our needs, it is: comm. When you meet the above questions, "comm" should be your first choice
comm [ -123 ] file1 file2
comm will read file1 and file2 and generate three columns of output: lines only in file1; lines only in file2; and lines in both files. For detailed explanation, pls man comm.
1) Print out the lines that are only contained in file1?
bash-2.03$ comm -23 file1 file2
11111111
33333333
55555555
77777777
99999999
2) Print out the lines that are only contained in file2?
bash-2.03$ comm -13 file1 file2
00000000
3) Print out the lines that are contained both in file1 and file2
bash-2.03$ comm -12 file1 file2
22222222
44444444
66666666
88888888
Besides the comm, we still have various ways to finish the above tasks.
1) Print out the lines that are only contained in file1?
diff file1 file2 | grep "^<"|sed 's/^< //g'
for i in $(<file1); do (grep $i file2)||echo $i>>temp ; done;
cat temp
In comparison, comm is much easier to remember. 作者: ZealeS 時(shí)間: 2005-12-16 09:51
我的記憶是判斷那個(gè)文件沒有的,就把參數(shù)去掉。都有的才
file1中有file2中沒有 comm -23 #(file1有,把1去掉)
file1沒有有file2中有 comm -13 #(file2有,把2去掉)
file1和file2都存在 comm -12 #(都有,把3去掉)作者: 寂寞烈火 時(shí)間: 2005-12-16 12:09
使用comm需要注意的是下面的紅字
compare two sorted files line by line作者: icesummit 時(shí)間: 2005-12-16 13:19
原帖由 寂寞烈火 于 2005-12-16 12:09 發(fā)表
使用comm需要注意的是下面的紅字
compare two sorted files line by line
Right!作者: 大螞蟻 時(shí)間: 2005-12-16 13:43
我習(xí)慣用win下的UltraEdit 沒排序的一樣比較 作者: dbcat 時(shí)間: 2005-12-16 13:47
嘻,學(xué)習(xí)!作者: noever 時(shí)間: 2007-11-04 21:44
thanks a lot