我有两个文件
第一个文件如下:
500006011000.S3S.mockup
500007011000.S3S.mockup
500008011000.S3S.mockup
500009011000.S3S.mockup
500010011000.S3S.mockup
500012011000.S3S.mockup
500014011000.S3S.mockup
500016011000.S3S.mockup
500134010100.S3S.mockup
500028011201.S3S.mockup
500129010200.S3S.mockup
500142010100.S3S.mockup
500144010100.S3S.mockup
500015011800.S3S.mockup
第二个文件看起来
500006011000.S3S
500006011000.S3S
500007011000.S3S
500008011000.S3S
500009011000.S3S
500010011000.S3S
500011011000.S3S
500012011000.S3S
500014011000.S3S
500015011800.S3S
500016011000.S3S
500018011000.S3S
500022010400.S3S
500028011201.S3S
500031010200.S3S
500032030200.S3S
500040020500.S3S
500063011000.S3S
500067010001.S3S
500072010400.S3S
500099010300.S3S
500129010200.S3S
500134010100.S3S
500142010100.S3S
500144010100.S3S
500146010100.S3S
500147010100.S3S
Contents.lst
gen_sch_list.txt
我可以使用此代码进行比较,然后输出相同的内容
findstr /ixg:C:\CA_MOCKUP\first.txt C:\CA_MOCKUP\second.txt > C:\CA_MOCKUP\result.txt
通常,只有在第一个txt删除每一行的.mockup时,它才有效。
我们可以仅根据字符串的前6位来比较两个文件吗?这会适应很多情况下可能出现的许多问题。
答案 0 :(得分:2)
恐怕您的问题很令人困惑。 findstr
命令不会比较文件;它找到字符串。 (顺便说一句,请参见fc
命令)。另外,您没有指定要“相同内容”的文件...
下面的代码从first.txt
文件中提取行,其前六个字符也出现在second.txt
的任何行中的前六个字符中文件:
@echo off
setlocal EnableDelayedExpansion
rem Create the "key" array with the first six chars from second.txt file
for /F "delims=" %%a in (second.txt) do (
set "line=%%a"
set "key[!line:~0,6!]=1"
)
rem Extract the lines in first.txt file with the same key
(for /F "delims=" %%a in (first.txt) do (
set "line=%%a"
set "s6=!line:~0,6!"
if defined key[!s6!] echo !line!
)) > result.txt
答案 1 :(得分:0)
对于仅收集每行的前6个字符,我们可以使用一些for
循环以及syntax-substring。我们可以简单地将此修改后的数据存储在新的临时文件中,以便稍后与findstr
进行比较。
CompareFirst6Chars.bat:
@echo off
@setlocal enabledelayedexpansion
Rem | Configuration
Set "CompaireFile1=1.txt"
Set "CompaireFile2=2.txt"
Set "ResultFile=result.txt
Rem | Loop For Each Item
for %%A in (!CompaireFile1! !CompaireFile2!) do (
Rem | Grab Only First 6 Characters From Files, Format In Modified
for /f "Tokens=*" %%B in (%%A) do (
Rem | Save & Modify Strings
Set "Modified=%%B"
Set "Modified=!Modified:~0,6!"
Rem | Save To File
Echo !Modified!>> %%A.temp
)
)
Rem | Compare & Extract Results
findstr /ixg:!CompaireFile1!.temp !CompaireFile2!.temp > !ResultFile!
Rem | Remove Temp Files
Del "!CompaireFile1!.temp" "!CompaireFile2!.temp"
goto :EOF
这将为我们提供以下输出:
500006
500006
500007
500008
500009
500010
500012
500014
500015
500016
500028
500129
500134
500142
500144
另一种解决方案是从字符串中完全删除.mockup
。为此,该过程非常模拟,但我们使用syntax-substring代替syntax-replace。这将使我们将所有.mockup
都替换为空。
RemoveMockupCompareFiles.bat:
@echo off
@setlocal enabledelayedexpansion
Rem | Configuration
Set "CompaireFile1=1.txt"
Set "CompaireFile2=2.txt"
Set "ResultFile=result.txt
Rem | Loop For Each Item
for %%A in (!CompaireFile1! !CompaireFile2!) do (
Rem | Grab Only First 6 Characters From Files, Format In Modified
for /f "Tokens=*" %%B in (%%A) do (
Rem | Save & Modify Strings
Set "Modified=%%B"
Set "Modified=!Modified:.mockup=!"
Rem | Save To File
Echo !Modified!>> %%A.temp
)
)
Rem | Compare & Extract Results
findstr /ixg:!CompaireFile1!.temp !CompaireFile2!.temp > !ResultFile!
Rem | Remove Temp Files
Del "!CompaireFile1!.temp" "!CompaireFile2!.temp"
goto :EOF
这将为我们提供以下输出:
500006011000.S3S
500006011000.S3S
500007011000.S3S
500008011000.S3S
500009011000.S3S
500010011000.S3S
500012011000.S3S
500014011000.S3S
500015011800.S3S
500016011000.S3S
500028011201.S3S
500129010200.S3S
500134010100.S3S
500142010100.S3S
500144010100.S3S
要获取有关任何命令的帮助,请执行以下操作:
call /?
set /?
for /?
if /?
find /?