在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在项目中遇到一个奇怪的bug,是由一行简单代码引起的。 复制代码 代码如下:# temp1.txt文件内容 $> cat temp1.txt 20110224 20110225 20110228 20110301 20110302 # temp2.txt文件内容 $> cat temp2.txt 20110228 20110301 20110302 20110303 20110304 # diff命令输出结果 复制代码 代码如下:$> diff temp1.txt temp2.txt 1,2d0 < 20110224 < 20110225 5a4,5 > 20110303 > 20110304 # 只输出temp2.txt文件独有的内容 $> diff temp1.txt temp2.txt | grep "> " | sed 's/> //g' 20110303 20110304 说明:输出结果去掉了两个文件的共同内容,只输出了temp2.txt的新增部分,和预想的结果一样。 复制代码 代码如下:$> 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),也同时输出了两个文件的共同内容(20110228-20110303),从而导致了与预期不一致的结果。 复制代码 代码如下:$> comm -13 temp1.txt temp2.txt 20110304 20110307 20110308 20110309 20110310 20110311 20110314 comm命令用来比较两个文件,具体用法: 复制代码 代码如下:$> 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恢复temp2文件 $> patch -i temp.diff -o temp2_restore.txt temp1.txt Looks like a normal diff. done # 完成后temp2_restore和原temp2文件内容一致 $> cat temp2_restore.txt 20110228 20110301 20110302 20110303 20110304 |
请发表评论