标题: [文本处理] [己解决]批处理怎样对比文本输出不同行? [打印本页]
作者: keshun 时间: 2011-10-30 16:37 标题: [己解决]批处理怎样对比文本输出不同行?
本帖最后由 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。
作者: weichenxiehou 时间: 2011-10-30 17:35
回复 1# keshun - @echo off
- findstr /g:1.txt 2.txt>4.txt
- findstr /vg:1.txt 2.txt>3.txt
- findstr /vg:2.txt 1.txt>>3.txt
复制代码
作者: keshun 时间: 2011-10-30 19:03
刚试过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的基础上更改几个日期,这样处理就出错了。
作者: keshun 时间: 2011-10-30 19:04
刚试过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的基础上更改几个日期,这样处理就出错了。
作者: weichenxiehou 时间: 2011-10-30 19:37
回复 4# keshun
我试了怎么可以呢?
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基础上改了三个时间:- name=杨 datestop=2011-08-07 ip=3511杨 active=yes prio=0
- name=李超 datestop=2011-08-10 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-01 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-30 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
复制代码
测试结果:- f:\test>findstr /g:1.txt 2.txt
- name=杨 datestop=2011-08-07 ip=3511杨 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-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-11 ip=3556孙威 active=yes prio=0
- name=麦甜 datestop=2011-08-14 ip=3630麦甜 active=yes prio=0
-
- f:\test>findstr /vg:1.txt 2.txt
- name=李超 datestop=2011-08-10 ip=2555李超 active=yes prio=0
- name=蒋科 datestop=2011-08-01 ip=2544蒋科 active=yes prio=0
- name=周林 datestop=2011-08-30 ip=3550周林 active=yes prio=0
-
- f:\test>findstr /vg:2.txt 1.txt
- name=李超 datestop=2011-08-01 ip=2555李超 active=yes prio=0
- name=蒋科 datestop=2011-08-10 ip=2544蒋科 active=yes prio=0
- name=周林 datestop=2011-08-16 ip=3550周林 active=yes prio=0
复制代码
作者: keshun 时间: 2011-10-30 20:13
本帖最后由 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
作者: sxw 时间: 2011-10-30 20:33
本帖最后由 sxw 于 2011-10-30 20:40 编辑
用Perl写了下,比较粗糙,应该可以实现你的要求吧?- #!/usr/bin/perl
- #diff.pl
- #usage:perl diff.pl 1.txt 2.txt
- use strict;
- use warnings;
- my %hash;
- my @file;
- my $fh;
- open $fh,"<","1.txt" or die;
- @file=<$fh>;
- close $fh;
- my $fh_2;
- open $fh_2,"<","2.txt" or die;
- my @file_2=<$fh_2>;
- close $fh_2;
- push @file,@file_2;
- chomp @file;
- $hash{"$_"}++ for (@file);
- open FH,">","3.txt" or die;
- open F,">","4.txt" or die;
- for (keys %hash) {
- print $hash{$_},"\n";
- print FH "$_\n" if $hash{$_}==1;
- print F "$_\n" if $hash{$_}>1;
- }
复制代码
作者: sxw 时间: 2011-10-30 20:57
来个简化一点的的:
perl test.pl 1.txt 2.txt 或
perl test.pl 2.txt 1.txt- #!/usr/bin/perl
- use strict;
- use warnings;
- my %hash;
- while (<ARGV>) {
- chomp;
- $hash{"$_"}++;
- }
- open FH,">","3.txt" or die;
- open F,">","4.txt" or die;
- for (keys %hash) {
- print FH "$_\n" if $hash{$_}<=1;
- print F "$_\n" if $hash{$_}>1;
- }
复制代码
作者: awk 时间: 2011-10-30 22:20
回复 8# sxw
我也来一个:- gawk "FNR==NR{a[$0]++}FNR<NR{if(a[$0])print >\"d.txt\";else print >\"c.txt\"}" a.txt b.txt
复制代码
作者: sxw 时间: 2011-10-30 23:21
回复 9# awk
这,也太短了 ,哈哈
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |