如何比较两个文本文件(不包括每行的前 5 个字符)?

问题描述 投票:0回答:2

我正在尝试比较两个文件。除了前 5 个字符之外,它们应该相同。我需要错误文件来包含不匹配的行。有没有办法限制 findstr 命令只查看位置 6 及之后的字符?

@echo off
echo Start %date% %time%
REM Verify that File1.txt and File2.txt are identical except for the first 5 characters
findstr /vio File1.txt File2.txt > Berror.txt
echo End %date% %time%
Berror.txt
Pause

文件1.txt

00000spider
00000center
00000earwax
00000horn
00000cup
00000ton
00000ankle
00000foster
00000restaurant
00000ecstasy

文件2.txt

46884spider
12464center
31197earwax
32514horn
64856cup
61465ton
91468anklet
31515foster
91580restaurant
41623ecstasy

我尝试过将 /r 与 [6-20] 一起使用,但我找不到语法示例,所以我真的不知道如何使用它。我期望的输出是第 7 行:91468anklet 和 00000ankle,因为这是本示例中唯一有区别的行。

batch-file compare findstr
2个回答
0
投票

一种解决方案是在比较之前拆分.txt 文件。在这种情况下,需要

cut -c 9-
来截断一行中的数字。 埃杰:

echo "00121314JohnSmith" | cut -c 9-
JohnSmith

0
投票
@echo off
setlocal EnableDelayedExpansion

< file2.txt (
   for /F "tokens=1,* delims=:" %%a in ('findstr /N "^" file1.txt') do (
      set "line1=%%b"
      set /P "line2="
      if "!line1:~5!" neq "!line2:~5!" (
         echo Line # %%a:
         echo File1: !line1!
         echo File2: !line2!
      )
   )
)

输出:

Line # 7:
File1: 00000ankle
File2: 91468anklet
© www.soinside.com 2019 - 2024. All rights reserved.