免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3406 | 回复: 8
打印 上一主题 下一主题

[文本处理] 比较两个文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-10-31 10:20 |只看该作者 |倒序浏览
有连个文件file1,file2两个文件的内容存放的都是文件路径及状态如:
/mstr/mstr=A
/mstr/mstr/auxfiles=A
/mstr/mstr/auxfiles/zip=A
/mstr/mstr/auxfiles/zip/MicroStrategy.zip=A
等号右边是文件的状态可以是A,U,D三种,如过两个文件相同就什么也不输出,如果只是文件相同,状态不同,打印出比较结果,file1中文件/mstr/msrt在file2中的状态为D
,如果一个文件在file1中存在,在file2中不存在,就打印结果file1中文件/msrt/mstr在文file2中不存在,
谁能用shell 帮忙处理下,谢谢

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
2 [报告]
发表于 2013-10-31 10:45 |只看该作者
  1. cut -d = -f1 file1 file2|sort -u > file3

  2. awk -F = 'FILENAME=="file1"{a[$1]=$2}
  3.           FILENAME=="file2"{b[$1]=$2}
  4.           FILENAME=="file3"{ {if ($1 in a && $1 in b)
  5.                                 {if (a[$1]!=b[$1]) print $1 " in file1 is " a[$1] ", file2 is " b[$1]}
  6.                               else
  7.                                 { if ($1 in a) {print $1 " is not exist in file2"}
  8.                                   else {print $1 " is not exist in file1"}
  9.                                 }
  10.                               }
  11.                                  
  12.                             }' file1 file2 file3
复制代码

论坛徽章:
15
CU大牛徽章
日期:2013-03-13 15:32:35白羊座
日期:2013-10-30 13:11:16未羊
日期:2013-11-29 10:47:02白羊座
日期:2014-01-22 11:34:25白羊座
日期:2014-03-04 11:05:50申猴
日期:2014-07-29 16:31:36天秤座
日期:2013-10-23 15:33:47未羊
日期:2013-10-09 09:18:53CU大牛徽章
日期:2013-03-13 15:38:15CU大牛徽章
日期:2013-03-13 15:38:52CU大牛徽章
日期:2013-03-14 14:08:55未羊
日期:2013-09-23 09:29:22
3 [报告]
发表于 2013-10-31 10:54 |只看该作者
diff可以实现你的要求。

论坛徽章:
36
摩羯座
日期:2013-09-23 16:37:312015年亚洲杯之沙特阿拉伯
日期:2015-04-14 09:10:172015亚冠之柏太阳神
日期:2015-06-25 08:48:212015亚冠之武里南联
日期:2015-07-28 09:01:082015亚冠之莱赫维亚
日期:2015-07-28 15:44:172015亚冠之柏斯波利斯
日期:2015-09-06 14:08:52白银圣斗士
日期:2015-11-25 17:06:2815-16赛季CBA联赛之吉林
日期:2015-12-09 16:59:072016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之辽宁
日期:2016-04-14 09:29:04luobin
日期:2016-06-17 17:46:3615-16赛季CBA联赛之天津
日期:2016-08-16 14:11:01
4 [报告]
发表于 2013-10-31 11:29 |只看该作者
本帖最后由 LikeLx 于 2013-10-31 16:17 编辑

试试这个行不
  1. awk -F "=" 'NR==FNR{a[$1]=$2;next}{if($1 in a) ;else {a[$1]="";print $1,"is not exist in c.txt"} if(a[$1]&&$1 in a&&a[$1]!=$2) print $1 " in c.txt is",a[$1]}' c.txt b.txt
  2. /mstr/mstr/auxfiles/zip in c.txt is U
  3. /mstr/mstr/test is not exist in c.txt
  4. [root@dbserver ~]# cat b.txt c.txt
  5. /mstr/mstr=A
  6. /mstr/mstr/auxfiles=A
  7. /mstr/mstr/auxfiles/zip=A
  8. /mstr/mstr/auxfiles/zip/MicroStrategy.zip=A
  9. /mstr/mstr/test=A
  10. /mstr/mstr=A
  11. /mstr/mstr/auxfiles=A
  12. /mstr/mstr/auxfiles/zip=U
  13. /mstr/mstr/auxfiles/zip/MicroStrategy.zip=A
复制代码

论坛徽章:
0
5 [报告]
发表于 2013-10-31 13:12 |只看该作者
diff 不可以的

   

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
6 [报告]
发表于 2013-10-31 13:25 |只看该作者
回复 5# sea_tar


    其实你只需要教会别人怎么读diff命令的结果就行了

论坛徽章:
0
7 [报告]
发表于 2013-10-31 14:56 |只看该作者
谢谢,还有一个问题,就是处理一个文件file,内容如下
/mstr/mstr=A
/mstr/mstr/a.txt=A
/mstr/mstr/auxfiles=A
/mstr/mstr/auxfiles/zip=A
/mstr/mstr/auxfiles/zip/MicroStrategy.zip=A

/mstr/mstr2=D
/mstr/mstr2/auxfiles2=D
/mstr/mstr2/auxfiles/zip=D
/mstr/mstr2/auxfiles/zip/MicroStrategy.zip=D
/mstr/mstr2/a.txt=D
如果后面状态是A,同时这个路径是目录,不是文件,并且这个目录下有文件就把这个目录去掉,如果只是一个目录,就保留,意思就是滞留该目录下最深层次那个路径,比如这个 文件要留/mstr/mstr/a.txt=A
/mstr/mstr/auxfiles=A
/mstr/mstr/auxfiles/zip=A
/mstr/mstr/auxfiles/zip/MicroStrategy.zip=A
因为 /mstr/mstr下有文件或者目录
如果后面是D,只保留最外层目录,能不能帮帮忙啊




   

论坛徽章:
0
8 [报告]
发表于 2013-11-04 09:24 |只看该作者
回复 4# LikeLx


   

论坛徽章:
0
9 [报告]
发表于 2013-11-04 09:25 |只看该作者
回复 2# rdcwayx
谢谢,还有一个问题,就是处理一个文件file,内容如下
/mstr/mstr=A
/mstr/mstr/a.txt=A
/mstr/mstr/auxfiles=A
/mstr/mstr/auxfiles/zip=A
/mstr/mstr/auxfiles/zip/MicroStrategy.zip=A

/mstr/mstr2=D
/mstr/mstr2/auxfiles2=D
/mstr/mstr2/auxfiles/zip=D
/mstr/mstr2/auxfiles/zip/MicroStrategy.zip=D
/mstr/mstr2/a.txt=D
如果后面状态是A,同时这个路径是目录,不是文件,并且这个目录下有文件就把这个目录去掉,如果只是一个目录,就保留,意思就是滞留该目录下最深层次那个路径,比如这个 文件要留/mstr/mstr/a.txt=A
/mstr/mstr/auxfiles=A
/mstr/mstr/auxfiles/zip=A
/mstr/mstr/auxfiles/zip/MicroStrategy.zip=A
因为 /mstr/mstr下有文件或者目录
如果后面是D,只保留最外层目录,能不能帮帮忙啊



   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP