Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 搜索两个字符串,然后将结果添加到一起_Batch File - Fatal编程技术网

Batch file 搜索两个字符串,然后将结果添加到一起

Batch file 搜索两个字符串,然后将结果添加到一起,batch-file,Batch File,我试图创建一个批处理文件,在一个文件中搜索两个不同的字符串。然后将第一次搜索的第一行和第二次搜索的第一行合并为一行 我意识到这远远超过了我的批量技能,所以如果能提供一点帮助,我将不胜感激 提前谢谢 例如: file.txt: hello i'm a line with word1 hello i'm a line with word2 hello i'm another line with word1 hello i'm another line with word2 hello i'm

我试图创建一个批处理文件,在一个文件中搜索两个不同的字符串。然后将第一次搜索的第一行和第二次搜索的第一行合并为一行

我意识到这远远超过了我的批量技能,所以如果能提供一点帮助,我将不胜感激

提前谢谢



例如:

file.txt:

hello i'm a line with word1 
hello i'm a line with word2
hello i'm another line with word1
hello i'm another line with word2
hello i'm yet another line with word1
hello i'm yet another line with word2
result.txt:

hello i'm a line with word1hello i'm a line with word2 
hello i'm another line with word1hello i'm another line with word2
hello i'm yet another line with word1hello i'm yet another line with word2

设置要在变量
PairsToShow
中显示的线对数:

@ECHO OFF &SETLOCAL
SET "FileName=file.txt"
SET "Word1=word1"
SET "Word2=word2"
SET /a PairsToShow=3

SET /a Lines1=0, Lines2=0
FOR /f "delims=" %%a IN ('findstr "%Word1%" "%FileName%"') DO (
    SET "str=%%a"
    SET /a Lines1+=1
    SETLOCAL enabledelayedexpansion
    SET "$1!Lines1!=!str!"
    FOR /f "tokens=1*delims==" %%b IN ('set "$1"') DO (IF "!"=="" endlocal)&SET "%%b=%%c"
)
FOR /f "delims=" %%a IN ('findstr "%Word2%" "%FileName%"') DO (
    SET "str=%%a"
    SET /a Lines2+=1
    SETLOCAL enabledelayedexpansion
    SET "$2!Lines2!=!str!"
    FOR /f "tokens=1*delims==" %%b IN ('set "$2"') DO (IF "!"=="" endlocal)&SET "%%b=%%c"
)
SET /a Lines=Lines1+Lines2
ECHO(%Lines% lines read from %FileName%.
IF %Lines1% leq %Lines2% (SET /a MaxPairs=Lines1) ELSE SET /a MaxPairs=Lines2
IF %PairsToShow% gtr %MaxPairs% (
    ECHO only text for %MaxPairs% pairs NOT %PairsToShow% :/
    GOTO :END
)
(FOR /l %%a IN (1,1,%PairsToShow%) DO (
    SETLOCAL ENABLEDELAYEDEXPANSION
    CALL SET "Line1=%%$1%%a%%"
    CALL SET "Line2=%%$2%%a%%"
    <NUL SET /p "=!Line1!"
    ECHO !Line2!
    ENDLOCAL
))> result.txt
ENDLOCAL
TYPE result.txt
:END
PAUSE
@ECHO OFF&SETLOCAL
设置“FileName=file.txt”
设置“Word1=Word1”
设置“Word2=Word2”
设置/a PairsToShow=3
设置/a行1=0,行2=0
对于/f“delims=“%%a IN('findstr”%Word1%“”“%FileName%”)DO(
设置“str=%%a”
设置/a行1+=1
SETLOCAL enabledelayedexpansion
设置“$1!Lines1!=!str!”
对于/f“tokens=1*delims==”%b IN('set“$1“')DO(如果“!”==“endlocal”)&设置“%%b=%c”
)
对于/f“delims=“%%a IN('findstr”%Word2%“”“%FileName%”)DO(
设置“str=%%a”
设置/a行2+=1
SETLOCAL enabledelayedexpansion
设置“$2!Lines2!=!str!”
对于/f“tokens=1*delims==”%b IN('set“$2“')DO(如果“!”==“endlocal”)&设置“%%b=%c”
)
设置/a行=行1+行2
回显(%Lines%)从%FileName%读取的行数。
如果%Lines1%leq%Lines2%(设置/a MaxPairs=Lines1),则设置/a MaxPairs=Lines2
如果%PairsToShow%gtr%MaxPairs%(
仅回显%MaxPairs%对而不是%PairsToShow%的文本:/
后藤:结束
)
(对于(1,1,%PairsToShow%)中的/l%%a(
SETLOCAL ENABLEDELAYEDEXPANSION
调用集“Line1=%%$1%%a%%”
调用集“Line2=%%$2%%a%%”
result.txt
端部
键入result.txt
:结束
暂停

向我们展示您目前所拥有的。那么“为我编写代码”不是免费的吗网站类型。因此,我的作品在瞬间编辑了这两个词,它们是固定的,还是来自
file.txt
?你的意思是什么?这些词存在于file.txt中,但我选择了要搜索的词。你是否被限制为批处理?在PowerShell甚至VBScript中执行此操作会容易得多。我得到了所有带有“word1”和“word2”的行但是我需要word1搜索的第一行和word2搜索的第一行是一行示例:file.txt:hello我是word1的一行hello我是word2结果的一行。txt:hello我是word1的一行hello我是word1的一行word2@user2553264您可以尝试我的编辑:)谢谢你的回答!但我只得到以下信息:file.txt:你好,我是result.txt中的一行字1。txt:你好,我是result.txt中的一行字2。它对你有用吗?现在你得到了“找到单词的第一行”。不对吗?是的!但是result.txt中的第一行应该是“找到单词的第一行”+“找到单词的第一行字2”
@ECHO OFF &SETLOCAL
SET "FileName=file.txt"
SET "Word1=word1"
SET "Word2=word2"
SET /a PairsToShow=3

SET /a Lines1=0, Lines2=0
FOR /f "delims=" %%a IN ('findstr "%Word1%" "%FileName%"') DO (
    SET "str=%%a"
    SET /a Lines1+=1
    SETLOCAL enabledelayedexpansion
    SET "$1!Lines1!=!str!"
    FOR /f "tokens=1*delims==" %%b IN ('set "$1"') DO (IF "!"=="" endlocal)&SET "%%b=%%c"
)
FOR /f "delims=" %%a IN ('findstr "%Word2%" "%FileName%"') DO (
    SET "str=%%a"
    SET /a Lines2+=1
    SETLOCAL enabledelayedexpansion
    SET "$2!Lines2!=!str!"
    FOR /f "tokens=1*delims==" %%b IN ('set "$2"') DO (IF "!"=="" endlocal)&SET "%%b=%%c"
)
SET /a Lines=Lines1+Lines2
ECHO(%Lines% lines read from %FileName%.
IF %Lines1% leq %Lines2% (SET /a MaxPairs=Lines1) ELSE SET /a MaxPairs=Lines2
IF %PairsToShow% gtr %MaxPairs% (
    ECHO only text for %MaxPairs% pairs NOT %PairsToShow% :/
    GOTO :END
)
(FOR /l %%a IN (1,1,%PairsToShow%) DO (
    SETLOCAL ENABLEDELAYEDEXPANSION
    CALL SET "Line1=%%$1%%a%%"
    CALL SET "Line2=%%$2%%a%%"
    <NUL SET /p "=!Line1!"
    ECHO !Line2!
    ENDLOCAL
))> result.txt
ENDLOCAL
TYPE result.txt
:END
PAUSE