Board logo

标题: 如何分离出以下两个文件中不同的内容 [打印本页]

作者: xxniao     时间: 2006-2-28 04:50    标题: 如何分离出以下两个文件中不同的内容

A文件.txt

123456.exe
abcdef.exe
zghcge.exe

B文件.txt
123456.exe
abcdef.exe
zghcge.exe
aaaaaa.exe
cccccc.exe
dddddd.exe

将A和B文件对比分离出以下不相同的内容
aaaaaa.exe
cccccc.exe
dddddd.exe

请高手赐教。。。。。
作者: 3742668     时间: 2006-2-28 08:27
findstr /v /g:a.txt b.txt
XP专业版,SP2下测试通过。。
当然你也可以用for来循环。。
作者: xxniao     时间: 2006-2-28 12:16
好像二楼的答案离题了吧!!
用 findsrv 是用来查找的啊,而不是比较吧!!
作者: xxniao     时间: 2006-2-28 12:21
能不能说说用FOR如何来实现吗?
作者: 3742668     时间: 2006-2-28 15:56


  Quote:
Originally posted by xxniao at 2006-2-28 12:16:
好像二楼的答案离题了吧!!
用 findsrv 是用来查找的啊,而不是比较吧!!

离题?
你自己好好看看你自己出的题 :   如何分离出以下两个文件中不同的内容
另外哪个规定findstr(不是findstv)只能用来查找?你要查找你去用FC和COMP去好了。
用最简单的方法实现想实现的功能就可以了,是不是不用for,不够麻烦就不算正解呢?建议你去学汇编去,用汇编来实现这个功能。。。
作者: willsort     时间: 2006-2-28 16:40
Re 3742668:

      兄的方法确实有效,然而此方法仅仅适用于b完全包含a中所有文本的情况,如果a中有b中不存在的文本行,那么这些行将不会记入输出结果;而如果文件a包含了文件b,则搜索结果将为空。

      另外,此方案要求a的体积不能过于庞大,否则将十分影响搜索的效率。同时a的文本行不能过于复杂,否则搜索将会出现错误结果[1]。

[1]批处理删除XP输入法问题!请dos高手解决
http://www.cn-dos.net/forum/viewthread.php?tid=19084
作者: 3742668     时间: 2006-2-28 19:14
呵呵,版主就是版主,看问题果然有深度。不过,如果你看了楼主前面发的帖子就可以大致了解到他要实现的功能可以就用我上面的那个功能完成。其实我认为只要能达到目的,用什么命令无所谓,不一定非要用for命令才显得水平高,有深度。下面是我最开始的版本,只不过想想没那么麻烦就一句findstr搞定,可惜楼主连试都没试,还弄了个findsrv出来。。。
@echo off
copy 1.txt a.txt
copy 2.txt b.txt
:find
set a=
set /p a=<a.txt
if "%a%" == "" goto end
findstr /v "%a%" a.txt > tmp.txt
type tmp.txt > a.txt
findstr /v "%a%" b.txt> tmp.txt
type tmp.txt >b.txt
goto find

:end
echo y | del a.txt
echo y | del b.txt
作者: 3742668     时间: 2006-2-28 19:27


  Quote:
Originally posted by willsort at 2006-2-28 16:40:
Re 3742668:

      兄的方法确实有效,然而此方法仅仅适用于b完全包含a中所有文本的情况,如果a中有b中不存在的文本行,那么这些行将不会记入输出砮..

看了这段话是吃也吃不好,睡也睡不好,苦思冥想终于凑了一篇出来,望willsort指正:
@echo off
if exist 1和2都有.txt echo y | del 1和2都有.txt
if exist 1有2没有.txt echo y | del 1有2没有.txt
if exist 2有1没有.txt echo y | del 2有1没有.txt
copy 1.txt tmp.txt

:same
set a=
set /p a=<tmp.txt
if "%a%" == "" goto different
findstr /v %a% tmp.txt > temp.txt
type temp.txt >tmp.txt
findstr %a% 2.txt >>tmp1.txt
goto same

:different
findstr /v /g:tmp1.txt 1.txt >1有2没有.txt
findstr /v /g:tmp1.txt 2.txt >2有1没有.txt

:end
ren tmp1.txt 1和2都有.txt
echo y | del tmp.txt
echo y | del temp.txt


ps:本来自认为算是比较精益求精,追求完美的人了,但是与兄相比,惭愧之至啊

[ Last edited by 3742668 on 2006-3-1 at 16:41 ]
作者: tigerpower     时间: 2006-2-28 22:51
还有一种情况是:同样的一行,A文件中有m行,而B文件中有n行(m!=n)。
要是提取出来的文件能反映这种情况就更完美了
作者: xxniao     时间: 2006-3-1 02:47
晕了,各位啊,对不起啊,,我这人技术实在是太差了,
刚刚上面兄弟说的用其它语言来实现,其实其它语言实现那是很简单的啊,
只是我现在就想用纯批处理啊,,,  其实你们说的很多东西我都不是很明白,
如果有得罪的地方请多多包含。
作者: 3742668     时间: 2006-3-1 16:55


  Quote:
Originally posted by tigerpower at 2006-2-28 22:51:
还有一种情况是:同样的一行,A文件中有m行,而B文件中有n行(m!=n)。
要是提取出来的文件能反映这种情况就更完美了

呵呵,谢谢提醒。实现其实不难,根据最后的  "1和2都有.txt"  和findstr 是可以了实现的,加上/n参数连在第几行都找得出.
作者: willsort     时间: 2006-3-2 17:17
Re xxniao:

      以上给出的所有方案代码都是使用“纯批处理”实现的,可在 NTCMD 下独立运行。

      [1]是我过去做过的一个比较目录文件的批处理,也许与楼主的要求有些相近。

Re 3742668:

      8楼的逐行查找的方案确可解决大体积文本的比较。但是复杂文本(如含有字符串 %\_ 的文件[2][3])的比较仍有研究。暂时发现的问题是,findstr 行中的 %a% 需要加引号,避免比较含空格行时出错。

Re tigerpower:

      你的问题似可用 "findstr /g:1.txt 2.txt >1和2都有.txt" 解决,未经测试。

[1] CompDir.cmd - Compare files in two directories.
:: CompDir.cmd - Compare files in two directories.
:: Will Sort - 19:40 2005-12-23 - CMD@WinXP
@echo off & setlocal EnableDelayedExpansion
if not "%2"=="" goto Start

:Help
echo CompDir.cmd - Compare files in two directories.
echo Usage: %0 [/f] dir1 dir2 [FileSpec]
echo   /f       Compare time/size/attrib of file
echo   filespec Specific files by wildcard of filename
goto :EOF

:Start
if /i "%1"=="/f" shift && set _CompFile=on
if "%3"=="" (set filespec=*.*) else set filespec=%3
for /r %~f1 %%f in (%filespec%) do (
    set file=%%~ff
    set file=!file:%~f1\=!
    if not exist "%~f2\!file!" echo.Only in 1: !file!
)
for /r %~f2 %%f in (%filespec%) do (
    set file=%%~ff
    set file=!file:%~f2\=!
    if not exist "%~f1\!file!" echo Only in 2: !file!
)
if not "%_CompFile%"=="on" goto :EOF
echo Compare Time/siZe/Attrib of file...
echo Press Ctrl+C to Quit
for /r %~f1 %%f in (%filespec%) do (
    set file=%%~ff
    set file=!file:%~f1\=!
    if exist "%~f2\!file!" for %%g in ("%~f2\!file!") do (        
        if "%%~tf" NEQ "%%~tg" echo !file! Time 1"%%~zf" NEQ 2"%%~zg">>"%temp%\_CompFile.tmp"
        if "%%~zf" NEQ "%%~zg" echo !file! Size 1"%%~zf" NEQ 2"%%~zg">>"%temp%\_CompFile.tmp"
        if "%%~af" NEQ "%%~ag" echo !file! Attrib 1"%%~zf" NEQ 2"%%~zg">>"%temp%\_CompFile.tmp"
    )
)
if exist "%temp%\_CompFile.tmp" del "%temp%\_CompFile.tmp"
[2]1.txt
%\_1
%\_2
%\_4
[3]2.txt
%\_1
%\_3
%\_4

作者: tigerpower     时间: 2006-3-2 20:24
willsort真不愧为板主,那些特殊情况偶是想都没想到。
还有一种解法是使用 comm :

@echo off
sort 1.txt>1__.txt
sort 2.txt>2__.txt
for %%i in (__ "" -1 -2) do comm 1.txt 2.txt %%i>result_%%i.txt
del *__.txt