校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > linux diff與comm命令比較文件(找出新增內(nèi)容)

linux diff與comm命令比較文件(找出新增內(nèi)容)

熱門標(biāo)簽:曲阜地圖標(biāo)注app 哪個地圖標(biāo)注更清晰 開封400電話辦理價格 凱立德劇院地圖標(biāo)注 天津人工電銷機(jī)器人費(fèi)用 漳州外呼系統(tǒng)怎么樣 地圖標(biāo)注小區(qū)項目入駐 內(nèi)蒙古電信外呼系統(tǒng) 4s店如何在百度地圖標(biāo)注

在項目中遇到一個奇怪的bug,是由一行簡單代碼引起的。
代碼作用:比較兩個UNIX文本文件,找出并打印文本2比文本1新增加的內(nèi)容。
代碼調(diào)用了diff命令,例如:
 
 

復(fù)制代碼
代碼如下:

# temp1.txt文件內(nèi)容
$> cat temp1.txt
20110224
20110225
20110228
20110301
20110302
# temp2.txt文件內(nèi)容
$> cat temp2.txt
20110228
20110301
20110302
20110303
20110304

# diff命令輸出結(jié)果


復(fù)制代碼
代碼如下:

$> diff temp1.txt temp2.txt
1,2d0
20110224
20110225
5a4,5
> 20110303
> 20110304
# 只輸出temp2.txt文件獨(dú)有的內(nèi)容
$> diff temp1.txt temp2.txt | grep "> " | sed 's/> //g'
20110303
20110304

說明:輸出結(jié)果去掉了兩個文件的共同內(nèi)容,只輸出了temp2.txt的新增部分,和預(yù)想的結(jié)果一樣。
 
但是,隨著temp1.txt文件內(nèi)容的增加,diff命令出現(xiàn)了不同預(yù)期的結(jié)果:


復(fù)制代碼
代碼如下:

$> cat temp1.txt
20101216
20101217
20101220
20101221
20101223
20101224
20101227
20101228
20101229
20101230
20101231
20110103
20110104
20110105
20110106
20110107
20110110
20110111
20110112
20110113
20110114
20110117
20110118
20110119
20110120
20110121
20110124
20110125
20110126
20110127
20110128
20110131
20110201
20110202
20110203
20110204
20110207
20110208
20110209
20110210
20110211
20110214
20110215
20110216
20110217
20110218
20110221
20110222
20110223
20110224
20110225
20110228
20110301
20110302
20110303
$> cat temp2.txt
20110228
20110301
20110302
20110303
20110304
20110307
20110308
20110309
20110310
20110311
20110314
$> diff temp1.txt temp2.txt
1,55c1,11
20101216
20101217
20101220
20101221
20101223
20101224
20101227
20101228
20101229
20101230
20101231
20110103
20110104
20110105
20110106
20110107
20110110
20110111
20110112
20110113
20110114
20110117
20110118
20110119
20110120
20110121
20110124
20110125
20110126
20110127
20110128
20110131
20110201
20110202
20110203
20110204
20110207
20110208
20110209
20110210
20110211
20110214
20110215
20110216
20110217
20110218
20110221
20110222
20110223
20110224
20110225
20110228
20110301
20110302
20110303
---
> 20110228
> 20110301
> 20110302
> 20110303
> 20110304
> 20110307
> 20110308
> 20110309
> 20110310
> 20110311
> 20110314
$> diff temp1.txt temp2.txt | grep "> " | sed 's/> //g'
20110228
20110301
20110302
20110303
20110304
20110307
20110308
20110309
20110310
20110311
20110314

可以看到,diff命令不但輸出了temp2.txt文件的新增部分(20110304-20110314),也同時輸出了兩個文件的共同內(nèi)容(20110228-20110303),從而導(dǎo)致了與預(yù)期不一致的結(jié)果。
查看diff命令的man手冊發(fā)現(xiàn),diff的作用是比較兩個文件的內(nèi)容,并輸出兩個文件之間的差異,產(chǎn)生一個能夠?qū)蓚€文件互相轉(zhuǎn)換的列表,但這個列表并不能100%保證是最小集。
于是,以上例子中,可以看到diff給出了temp1.txt和temp2.txt文件的比較差異結(jié)果,但其中包含了兩個文件的共同部分,因此與預(yù)期不一樣。
 
解決方法:
用comm命令代替diff,例如:


復(fù)制代碼
代碼如下:

$> comm -13 temp1.txt temp2.txt
20110304
20110307
20110308
20110309
20110310
20110311
20110314

comm命令用來比較兩個文件,具體用法:
comm [-123] file1 file2
-1 過濾file1獨(dú)有的內(nèi)容
-2 過濾file2獨(dú)有的內(nèi)容
-3 過濾file1和file2重復(fù)的內(nèi)容
 
備注:
diff的輸出格式,主要有以下幾種:
n1 a n3,n4
n1,n2 d n3
n1,n2 c n3,n4
例如"1,2d0" "5a4,5" "1,55c1,11"等。
其中n1和n2指第一個文件的行數(shù),n3和n4指第二個文件的行數(shù)。"a"代表add增加,"d"代表delete刪除,"c"代表change整塊變動。
有了diff的輸出結(jié)果,可以使用patch命令將一個文件恢復(fù)成另一個,例如:


復(fù)制代碼
代碼如下:

$> cat temp1.txt
20110224
20110225
20110228
20110301
20110302
$> cat temp2.txt
20110228
20110301
20110302
20110303
20110304
$> diff temp1.txt temp2.txt > temp.diff
$> cat temp.diff
1,2d0
20110224
20110225
5a4,5
> 20110303
> 20110304
# 使用temp.diff和temp1.txt恢復(fù)temp2文件
$> patch -i temp.diff -o temp2_restore.txt temp1.txt
Looks like a normal diff.
done
# 完成后temp2_restore和原temp2文件內(nèi)容一致
$> cat temp2_restore.txt
20110228
20110301
20110302
20110303
20110304

標(biāo)簽:三門峽 茂名 衡陽 仙桃 湘西 六盤水 衡陽 慶陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《linux diff與comm命令比較文件(找出新增內(nèi)容)》,本文關(guān)鍵詞  linux,diff,與,comm,命令,比較,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《linux diff與comm命令比較文件(找出新增內(nèi)容)》相關(guān)的同類信息!
  • 本頁收集關(guān)于linux diff與comm命令比較文件(找出新增內(nèi)容)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 南靖县| 溆浦县| 高陵县| 浦北县| 昌邑市| 南和县| 金川县| 奈曼旗| 南昌市| 巴塘县| 泰兴市| 潼关县| 青神县| 塘沽区| 广饶县| 客服| 余姚市| 新泰市| 张掖市| 锡林浩特市| 洞口县| 古田县| 大冶市| 长沙县| 田东县| 南丹县| 漳州市| 响水县| 昂仁县| 灌云县| 呼玛县| 罗源县| 石景山区| 定日县| 富顺县| 保靖县| 遵义县| 平果县| 禄劝| 乐至县| 车致|