我想使用cmd.exe' s fc
命令比较2个文本文件。但是,它也在不匹配线的上方和下方打印线。我该如何抑制这种行为?
A.TXT
32=10500.3000000 31=5252.8095 30=XXXX 75=20170208 00:32:40 6=5252.8095 60=20170208-00:00:03
b.txt
32=10500.3000000 31=5252.8095 30=YYYY 75=20170208 00:32:40 6=5252.8095 60=20170208-00:00:03
命令 - fc /l /n /c /t /lb200 a.txt b.txt1
输出 比较文件a.txt和B.TXT
***** a.txt 2: 31=5252.8095 3: 30=XXXX 4: 75=20170208 00:32:40 ***** B.TXT 2: 31=5252.8095 3: 30=YYYY 4: 75=20170208 00:32:40 *****
我想要什么
***** a.txt 3: 30=XXXX ***** B.TXT 3: 30=YYYY *****
答案 0 :(得分:2)
您可以将此作为起点
@echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=1,* delims=: eol=*" %%a in ('
fc /l /n /t /c 1.txt 2.txt
') do (
if defined _%%a (
set "line=%%b"
setlocal enabledelayedexpansion
if not !_%%a!==!line! (
echo(%%a: !_%%a!
echo(%%a: !line!
echo(
)
endlocal
set "_%%a="
) else set "_%%a=%%b"
)
在处理fc
命令的输出时,为每个行号定义一个变量。当找到相同的行号时,变量的内容将与新行进行比较,如果它们不同,则会回显两行。
答案 1 :(得分:0)
即使偏离所选标签的主题,在这些情况下,我总是推荐像windiff或WinMerge这样的工具并排比较。另见Gui-diff-tools。一些编辑提供类似TextPad的选项。
答案 2 :(得分:0)
不幸的是,fc
command的输出格式无法按照您的期望进行配置。
以下脚本仅搜索以*****
开头的行,仅跳过紧接在其之前或之后的每一行,但第一行始终不会被跳过。当然,在很多情况下,这种方法都失败了,但是对于简单的不同块,只要fc
能够同步,它就可以正常工作(因此,必须为/LB
指定足够的数量选项):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem /* Initialise variables, then loop over output of `fc` command,
rem with all command line arguments simply forwarded to `fc`: */
set "LINE=" & set "PREV=" & for /F "delims=" %%L in ('fc %*') do (
rem // This query is needed to skip lines after `*****`:
if not defined PREV (
rem /* This is just to not skip the very first line; just
rem comment it out if you do want to skip this line: */
if not defined LINE echo(%%L
rem // Store current line for next loop iteration:
set "PREV=%%L"
) else (
rem // Store current line:
set "LINE=%%L"
setlocal EnableDelayedExpansion
rem // Check if current line begins with `*****`:
if "!LINE:~,5!" == "*****" (
rem // Return current line but not the previous one:
echo(!LINE!
endlocal
set "PREV=%%L"
) else (
rem /* Current line does not begin with `*****`,
rem so check whether previous line does: */
if "!PREV:~,5!" == "*****" (
endlocal
rem /* Previous line begins with `*****`, hence
rem clear buffer for previous line in order to
rem let the following line be skipped then: */
set "PREV="
) else (
rem /* Neither the current nor the previous lines
rem begin with `*****`, so return the latter: */
echo(!PREV!
endlocal
set "PREV=%%L"
)
)
)
)
endlocal
exit /B
假设脚本名为filecomp.bat
,只需调用所有参数即可调用脚本,就像调用fc
一样,例如:
filecomp.bat /L /N /C /T /LB200 "a.txt" "b.txt"
根据您的示例数据,鉴于批处理文件以及示例数据文件都位于当前工作目录中,因此输出如下:
Comparing files a.txt and B.TXT ***** a.txt 3: 30=XXXX ***** B.TXT 3: 30=YYYY *****