Chinaunix

标题: 比较两个文件 [打印本页]

作者: sea_tar    时间: 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 帮忙处理下,谢谢
作者: rdcwayx    时间: 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
复制代码

作者: yzkarchive    时间: 2013-10-31 10:54
diff可以实现你的要求。
作者: LikeLx    时间: 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
复制代码

作者: sea_tar    时间: 2013-10-31 13:12
diff 不可以的

   
作者: Shell_HAT    时间: 2013-10-31 13:25
回复 5# sea_tar


    其实你只需要教会别人怎么读diff命令的结果就行了
作者: sea_tar    时间: 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,只保留最外层目录,能不能帮帮忙啊




   
作者: sea_tar    时间: 2013-11-04 09:24
回复 4# LikeLx


   
作者: sea_tar    时间: 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,只保留最外层目录,能不能帮帮忙啊



   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2