批处理文件将findstr结果的第一个匹配仅打印到文本文件
问题描述:
我试图编写一个从fileA.txt读取列表的批处理文件,如果匹配不存在,则检查fileC.txt如果匹配不存在只写入第一个匹配行fileB.txt到fileC.txt批处理文件将findstr结果的第一个匹配仅打印到文本文件
fileA.txt例如
aaa1
aaaa
aaaa4
bbb
ccc12
fileB.txt例如
aaa1 some text
aaa1 blah bla
aaa1 .r
aaaa some info
aaaa blah bla
aaaa4 some name
bbb some name to
bbb more blah blah
ccc12 another name
ccc12 blah bla
所得fileC.txt
aaa1 some text
aaaa some info
aaaa4 some name
bbb some name to
ccc12 another name
什么即时试图做
for /F %%i in (C:\filecopy\fileA.txt) do (
If exist (findstr /B /C:%%i fileC.txt) (
echo %%i exists) else (
findstr /B /C:%%i fileB.txt >> fileC.txt)
)
但是这些代码的心不是正确的和IM不知道如何最好地处理它
答
的解决方案是在fileC.txt只是的第一场比赛存储当fileA.txt中的每个单词在文件B.txt中搜索时(正如您在问题标题中指出的那样):
@echo off
setlocal
(for /F %%i in (fileA.txt) do (
set "firstMatch=true"
for /F "delims=" %%j in ('findstr /B /C:%%i fileB.txt') do (
if defined firstMatch (
echo %%j
set "firstMatch="
)
)
)) > fileC.txt
Tku正如我想要的那样工作:) –