Sac*_*tre 2 windows text copy batch-file
我是批处理脚本的新手.我对Batch Scripting知之甚少.
我怀疑的是如何只将一些行从文本文件复制到其他文本文件.
说我的File.txt是
This is sample file.
I want copy this line.
Also this line.
But not this line.
Run Code Online (Sandbox Code Playgroud)
我想复制第2行和第3行,但不使用它们的行号,因为可能会改变.
我到目前为止做的很多:
@ECHO OFF
SET InFile=abc.txt
SET OutFile=Output.txt
IF EXIST "%OutFile%" DEL "%OutFile%"
SET TempFile=Temp.txt
IF EXIST "%TempFile%" DEL "%TempFile%"
IF EXIST "%OutFile%" DEL "%OutFile%"
FOR /F "tokens=*" %%A IN ('FINDSTR "I want" "%InFile%"') DO (
ECHO.%%A> "%TempFile%"
ECHO.%TempFile%>>"%OutFile%"
REM CALL :RemovePrecedingWordA "%%A"
)
FOR /F "tokens=*" %%A IN ('FINDSTR " Also this" "%InFile%"') DO (
ECHO.%%A> "%TempFile%"
ECHO.%TempFile%>>"%OutFile%"
REM CALL :RemovePrecedingWordA "%%A"
)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.请帮忙.
您可以将/ g:选项用于findstr,基本上可以
findstr /g:pattern.txt %InFile% > %OutFile%
Run Code Online (Sandbox Code Playgroud)
其中pattern.txt是(在你的例子中)
I want
Also this
Run Code Online (Sandbox Code Playgroud)
这假设您可以findstr为要复制的所有行编写正则表达式.
如果您将findstr与多个文件(通配符)一起使用,您将获得前置文件名.为了解决这个问题,
del out.txt
for %F in (*.txt); do findstr /g:pattern.txt %F >> out.txt
请注意您应该将源文件放在模式和输出文件的不同目录中(或使用不同的扩展名),否则*.txt通配符也会选择这些文件.
| 归档时间: |
|
| 查看次数: |
21624 次 |
| 最近记录: |