查找字符串并将其整行复制到新文件中

时间:2018-06-01 07:06:31

标签: string batch-file find

我怎么能复制两行而不只是一行?

@echo off
setlocal enableextensions
set "source=input.txt"
set "target=output.txt"
pushd "%source%"
(for /f "tokens=1,* delims=:" %%a in ('findstr /i /l /c:"word123" "*.txt"') do (
echo(%%b
)) > "%target%"
popd

输入示例:

aaaaayr
vvvvvyr
9qeieir
qei9i9r
i9iqr9r
word123
s8ru9sr

示例输出:

i9iqr9r
word123

1 个答案:

答案 0 :(得分:0)

您可以使用FindStr搜索换行符:

@Echo Off
SetLocal DisableDelayedExpansion
Set "Src=input.txt"
Set "Dst=output.txt"
Set "Str=word123"

(Set LF=^
% 0x0A %
)
For /F %%A In ('Copy /Z "%~f0" Nul') Do Set "CR=%%A"
SetLocal EnableDelayedExpansion
Findstr /RC:".*!CR!*!LF!%Str%" /IC:"%Str%" "%Src%">"%Dst%"
EndLocal