windows 批处理文件将特定行从一个文本文件复制到另一个文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16280827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:28:31  来源:igfitidea点击:

Batch file to Copy particular lines from one text file to another text file

windowsbatch-filetextcopy

提问by Sachin Mhetre

I am very new to batch scripting. I don't have much knowledge about Batch Scripting.

我对批处理脚本很陌生。我对批处理脚本的了解不多。

My doubt is how to copy only some line from text file to other text file.

我的疑问是如何仅将文本文件中的某些行复制到其他文本文件。

Say my File.txt is

说我的 File.txt 是

This is sample file.
I want copy this line.
Also this line.
But not this line.

I want to copy line 2 and 3, but not using their line number, because may change.

我想复制第 2 行和第 3 行,但不使用它们的行号,因为可能会改变。

This muchI have done till now:

到目前为止我已经做了这么多:

@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"
    )

But its not working. Please help.

但它不起作用。请帮忙。

回答by Peter Hull

You could use the /g: option to findstr, basically do

您可以使用 /g: 选项来 findstr,基本上是这样做的

findstr /g:pattern.txt %InFile% > %OutFile%

where pattern.txt is (in your example)

pattern.txt 在哪里(在您的示例中)

I want
Also this

This assumes you can write findstrregular expressions for all the lines you want to copy.

这假设您可以findstr为要复制的所有行编写正则表达式。

If you use findstr with multiple files (wildcard) you will get the file name prepended. To get round this, use del out.txt for %F in (*.txt); do findstr /g:pattern.txt %F >> out.txt Note that you should put the source files in a different directory to the pattern and output files (or use a different extension) otherwise the *.txtwildcard will pick those files up, too.

如果您对多个文件(通配符)使用 findstr,您将获得文件名。要解决这个问题,请使用 del out.txt for %F in (*.txt); do findstr /g:pattern.txt %F >> out.txt 注意,您应该将源文件放在与模式和输出文件不同的目录中(或使用不同的扩展名),否则*.txt通配符也会选择这些文件。

回答by Erich Neugebauer

You can also use sedfor this purpose. Certain lines (in this case 2 and 3) are copied as follows:

您也可以将sed用于此目的。某些行(在本例中为 2 和 3)复制如下:

sed -n -e 2p -e 3p input.txt > output.txt

If you wish to copy a segment of lines (all lines from line 2 to line 10) then you may use:

如果您想复制一段行(从第 2 行到第 10 行的所有行),则可以使用:

sed -n -e 2,10p input.txt > output.txt

Or you can also use combinations of certain lines and segments:

或者您也可以使用某些行和段的组合:

sed -n -e 2,10p -e 15p -e 27p input.txt > output.txt