[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] [己解决]批处理怎样对比文本输出不同行?

本帖最后由 keshun 于 2011-10-30 23:33 编辑

文本1.txt
name=储 datestop=2011-10-27 ip=2195储 active=yes prio=0
name=王 datestop=2011-12-12 ip=2129王 active=yes prio=0
name=侯 datestop=2011-12-16 ip=2252侯 active=yes prio=0
name=李 datestop=2011-10-12 ip=2348李 active=yes prio=0
name=李 datestop=2011-12-10 ip=2160李 prio=0 active=yes
name=付 datestop=2011-12-02 ip=2172付 prio=0 active=yes

文本2.txt
name=储 datestop=2011-11-27 ip=2195储 active=yes prio=0
name=王 datestop=2011-12-12 ip=2129王 active=yes prio=0
name=黄 datestop=2011-11-05 ip=2279黄 active=yes prio=0
name=侯 datestop=2011-12-16 ip=2252侯 active=yes prio=0
name=李 datestop=2011-11-12 ip=2348李 active=yes prio=0
name=付 datestop=2011-12-02 ip=2172付 prio=0 active=yes
文本3.txt
name=储 datestop=2011-10-27 ip=2195储 active=yes prio=0
name=储 datestop=2011-11-27 ip=2195储 active=yes prio=0
name=黄 datestop=2011-11-05 ip=2279黄 active=yes prio=0
name=李 datestop=2011-10-12 ip=2348李 active=yes prio=0
name=李 datestop=2011-11-12 ip=2348李 active=yes prio=0
name=李 datestop=2011-12-10 ip=2160李 prio=0 active=yes
文本4.txt
name=王 datestop=2011-12-12 ip=2129王 active=yes prio=0
name=侯 datestop=2011-12-16 ip=2252侯 active=yes prio=0
name=付 datestop=2011-12-02 ip=2172付 prio=0 active=yes


要求批处理对比1.txt和2.txt,输出3.txt,也就是说只要1.txt和2.txt不相同的行都输出到3.txt,相同输出到4.txt。
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

回复 1# keshun
  1. @echo off
  2. findstr /g:1.txt 2.txt>4.txt
  3. findstr /vg:1.txt 2.txt>3.txt
  4. findstr /vg:2.txt 1.txt>>3.txt
复制代码
3

评分人数

看得多说得多,远比不上写得多。

TOP

刚试过2楼代码,处理举例文本没问题。不过批处我要的文本就会出错,不知为什么,望解决!!
文本1.txt
name=杨 datestop=2011-08-07 ip=3511杨 active=yes prio=0
name=李超 datestop=2011-08-01 ip=2555李超 active=yes prio=0
name=梁贤 datestop=2011-08-04 ip=3696梁贤 active=yes prio=0
name=刘铼 datestop=2011-08-03 ip=3673刘铼 active=yes prio=0
name=陈 datestop=2011-08-09 ip=3564陈 active=yes prio=0
name=尹良 datestop=2011-08-04 ip=3694尹良 active=yes prio=0
name=梁 datestop=2011-08-05 ip=3629梁 active=yes prio=0
name=闫平 datestop=2011-08-07 ip=2516闫平 active=yes prio=0
name=蒋科 datestop=2011-08-10 ip=2544蒋科 active=yes prio=0
name=谭伟 datestop=2011-08-19 ip=3640谭伟 prio=0 active=yes
name=石家 datestop=2011-08-14 ip=3544石家 active=yes prio=0
name=程洋 datestop=2011-08-19 ip=3642程洋 active=yes prio=0
name=周林 datestop=2011-08-16 ip=3550周林 active=yes prio=0
name=孙威 datestop=2011-08-11 ip=3556孙威 active=yes prio=0
name=麦甜 datestop=2011-08-14 ip=3630麦甜 active=yes prio=0

文本2.txt是在文本1.txt的基础上更改几个日期,这样处理就出错了。

TOP

刚试过2楼代码,处理举例文本没问题。不过批处我要的文本就会出错,不知为什么,望解决!!
文本1.txt
name=杨 datestop=2011-08-07 ip=3511杨 active=yes prio=0
name=李超 datestop=2011-08-01 ip=2555李超 active=yes prio=0
name=梁贤 datestop=2011-08-04 ip=3696梁贤 active=yes prio=0
name=刘铼 datestop=2011-08-03 ip=3673刘铼 active=yes prio=0
name=陈 datestop=2011-08-09 ip=3564陈 active=yes prio=0
name=尹良 datestop=2011-08-04 ip=3694尹良 active=yes prio=0
name=梁 datestop=2011-08-05 ip=3629梁 active=yes prio=0
name=闫平 datestop=2011-08-07 ip=2516闫平 active=yes prio=0
name=蒋科 datestop=2011-08-10 ip=2544蒋科 active=yes prio=0
name=谭伟 datestop=2011-08-19 ip=3640谭伟 prio=0 active=yes
name=石家 datestop=2011-08-14 ip=3544石家 active=yes prio=0
name=程洋 datestop=2011-08-19 ip=3642程洋 active=yes prio=0
name=周林 datestop=2011-08-16 ip=3550周林 active=yes prio=0
name=孙威 datestop=2011-08-11 ip=3556孙威 active=yes prio=0
name=麦甜 datestop=2011-08-14 ip=3630麦甜 active=yes prio=0

文本2.txt是在文本1.txt的基础上更改几个日期,这样处理就出错了。

TOP

回复 4# keshun
我试了怎么可以呢?
1.txt
  1. name=杨 datestop=2011-08-07 ip=3511杨 active=yes prio=0
  2. name=李超 datestop=2011-08-01 ip=2555李超 active=yes prio=0
  3. name=梁贤 datestop=2011-08-04 ip=3696梁贤 active=yes prio=0
  4. name=刘铼 datestop=2011-08-03 ip=3673刘铼 active=yes prio=0
  5. name=陈 datestop=2011-08-09 ip=3564陈 active=yes prio=0
  6. name=尹良 datestop=2011-08-04 ip=3694尹良 active=yes prio=0
  7. name=梁 datestop=2011-08-05 ip=3629梁 active=yes prio=0
  8. name=闫平 datestop=2011-08-07 ip=2516闫平 active=yes prio=0
  9. name=蒋科 datestop=2011-08-10 ip=2544蒋科 active=yes prio=0
  10. name=谭伟 datestop=2011-08-19 ip=3640谭伟 prio=0 active=yes
  11. name=石家 datestop=2011-08-14 ip=3544石家 active=yes prio=0
  12. name=程洋 datestop=2011-08-19 ip=3642程洋 active=yes prio=0
  13. name=周林 datestop=2011-08-16 ip=3550周林 active=yes prio=0
  14. name=孙威 datestop=2011-08-11 ip=3556孙威 active=yes prio=0
  15. name=麦甜 datestop=2011-08-14 ip=3630麦甜 active=yes prio=0
复制代码
2.txt在1.txt基础上改了三个时间:
  1. name=杨 datestop=2011-08-07 ip=3511杨 active=yes prio=0
  2. name=李超 datestop=2011-08-10 ip=2555李超 active=yes prio=0
  3. name=梁贤 datestop=2011-08-04 ip=3696梁贤 active=yes prio=0
  4. name=刘铼 datestop=2011-08-03 ip=3673刘铼 active=yes prio=0
  5. name=陈 datestop=2011-08-09 ip=3564陈 active=yes prio=0
  6. name=尹良 datestop=2011-08-04 ip=3694尹良 active=yes prio=0
  7. name=梁 datestop=2011-08-05 ip=3629梁 active=yes prio=0
  8. name=闫平 datestop=2011-08-07 ip=2516闫平 active=yes prio=0
  9. name=蒋科 datestop=2011-08-01 ip=2544蒋科 active=yes prio=0
  10. name=谭伟 datestop=2011-08-19 ip=3640谭伟 prio=0 active=yes
  11. name=石家 datestop=2011-08-14 ip=3544石家 active=yes prio=0
  12. name=程洋 datestop=2011-08-19 ip=3642程洋 active=yes prio=0
  13. name=周林 datestop=2011-08-30 ip=3550周林 active=yes prio=0
  14. name=孙威 datestop=2011-08-11 ip=3556孙威 active=yes prio=0
  15. name=麦甜 datestop=2011-08-14 ip=3630麦甜 active=yes prio=0
复制代码
测试结果:
  1. f:\test>findstr /g:1.txt 2.txt
  2. name=杨 datestop=2011-08-07 ip=3511杨 active=yes prio=0
  3. name=梁贤 datestop=2011-08-04 ip=3696梁贤 active=yes prio=0
  4. name=刘铼 datestop=2011-08-03 ip=3673刘铼 active=yes prio=0
  5. name=陈 datestop=2011-08-09 ip=3564陈 active=yes prio=0
  6. name=尹良 datestop=2011-08-04 ip=3694尹良 active=yes prio=0
  7. name=梁 datestop=2011-08-05 ip=3629梁 active=yes prio=0
  8. name=闫平 datestop=2011-08-07 ip=2516闫平 active=yes prio=0
  9. name=谭伟 datestop=2011-08-19 ip=3640谭伟 prio=0 active=yes
  10. name=石家 datestop=2011-08-14 ip=3544石家 active=yes prio=0
  11. name=程洋 datestop=2011-08-19 ip=3642程洋 active=yes prio=0
  12. name=孙威 datestop=2011-08-11 ip=3556孙威 active=yes prio=0
  13. name=麦甜 datestop=2011-08-14 ip=3630麦甜 active=yes prio=0
  14. f:\test>findstr /vg:1.txt 2.txt
  15. name=李超 datestop=2011-08-10 ip=2555李超 active=yes prio=0
  16. name=蒋科 datestop=2011-08-01 ip=2544蒋科 active=yes prio=0
  17. name=周林 datestop=2011-08-30 ip=3550周林 active=yes prio=0
  18. f:\test>findstr /vg:2.txt 1.txt
  19. name=李超 datestop=2011-08-01 ip=2555李超 active=yes prio=0
  20. name=蒋科 datestop=2011-08-10 ip=2544蒋科 active=yes prio=0
  21. name=周林 datestop=2011-08-16 ip=3550周林 active=yes prio=0
复制代码
看得多说得多,远比不上写得多。

TOP

本帖最后由 keshun 于 2011-10-30 20:14 编辑

把文本1.txt和2.txt改成如下,当文本1.txt和2.txt相同内容时也出错,我发现出错跟第一行是两个字或三个字的姓名相关。
name=杨俭 datestop=2011-08-07 ip=3511杨俭 active=yes prio=0
name=李远超 datestop=2011-08-01 ip=2555李远超 active=yes prio=0
name=梁圣贤 datestop=2011-08-04 ip=3696梁圣贤 active=yes prio=0
name=刘祖铼 datestop=2011-08-03 ip=3673刘祖铼 active=yes prio=0
name=陈亮 datestop=2011-08-09 ip=3564陈亮 active=yes prio=0
name=尹小良 datestop=2011-08-04 ip=3694尹小良 active=yes prio=0
name=梁珍 datestop=2011-08-05 ip=3629梁珍 active=yes prio=0
name=闫海平 datestop=2011-08-07 ip=2516闫海平 active=yes prio=0
name=蒋德科 datestop=2011-08-10 ip=2544蒋德科 active=yes prio=0
name=谭志伟 datestop=2011-08-19 ip=3640谭志伟 prio=0 active=yes
name=石国家 datestop=2011-08-14 ip=3544石国家 active=yes prio=0
name=程洋洋 datestop=2011-08-19 ip=3642程洋洋 active=yes prio=0
name=周竹林 datestop=2011-08-16 ip=3550周竹林 active=yes prio=0
name=孙昊威 datestop=2011-08-11 ip=3556孙昊威 active=yes prio=0
name=麦莉甜 datestop=2011-08-14 ip=3630麦莉甜 active=yes prio=0

TOP

本帖最后由 sxw 于 2011-10-30 20:40 编辑

用Perl写了下,比较粗糙,应该可以实现你的要求吧?
  1. #!/usr/bin/perl
  2. #diff.pl
  3. #usage:perl diff.pl 1.txt 2.txt
  4. use strict;
  5. use warnings;
  6. my %hash;
  7. my @file;
  8. my $fh;
  9. open $fh,"<","1.txt" or die;
  10. @file=<$fh>;
  11. close $fh;
  12. my $fh_2;
  13. open $fh_2,"<","2.txt" or die;
  14. my @file_2=<$fh_2>;
  15. close $fh_2;
  16. push @file,@file_2;
  17. chomp @file;
  18. $hash{"$_"}++ for (@file);
  19. open FH,">","3.txt" or die;
  20. open F,">","4.txt" or die;
  21. for (keys %hash) {
  22. print $hash{$_},"\n";
  23.     print FH "$_\n" if $hash{$_}==1;
  24. print F "$_\n" if $hash{$_}>1;
  25. }
复制代码

TOP

来个简化一点的的:
perl  test.pl  1.txt 2.txt 或
perl test.pl  2.txt  1.txt
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my %hash;
  5. while (<ARGV>) {
  6. chomp;
  7. $hash{"$_"}++;
  8. }
  9. open FH,">","3.txt" or die;
  10. open F,">","4.txt" or die;
  11. for (keys %hash) {
  12.     print FH "$_\n" if $hash{$_}<=1;
  13. print F "$_\n" if $hash{$_}>1;
  14. }
复制代码
1

评分人数

TOP

回复 8# sxw


我也来一个:
  1. gawk "FNR==NR{a[$0]++}FNR<NR{if(a[$0])print >\"d.txt\";else print >\"c.txt\"}" a.txt b.txt
复制代码
1

评分人数

TOP

回复 9# awk


    这,也太短了 ,哈哈

TOP

返回列表