Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.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_Scripting_Extract_Lines - Fatal编程技术网

Batch file 用于提取两个指定行之间的行的批处理脚本

Batch file 用于提取两个指定行之间的行的批处理脚本,batch-file,scripting,extract,lines,Batch File,Scripting,Extract,Lines,我有一个文本文件,希望使用windows批处理脚本提取两个指定行之间的所有行 第1行:!文件格式=广告 第二行:!版本=1.0 LineX:'父|子|主|***(该行以'开头,很长) 莱尼:!PropertyArray=成本中心(行以!) 莱恩兹 我想提取LineX和LineY之间的所有行,并将其输出到另一个文件 下面的代码正确地找到了起始行。但它只是删除了我想要停止脚本的行(Y行),并输出文件的其余部分 输出从X行到Z行,不带Y行 @for /f "tokens=1 delims=

我有一个文本文件,希望使用windows批处理脚本提取两个指定行之间的所有行

第1行:!文件格式=广告

第二行:!版本=1.0

LineX:'父|子|主|***(该行以'开头,很长)

莱尼:!PropertyArray=成本中心(行以!)

莱恩兹

我想提取LineX和LineY之间的所有行,并将其输出到另一个文件

下面的代码正确地找到了起始行。但它只是删除了我想要停止脚本的行(Y行),并输出文件的其余部分

输出从X行到Z行,不带Y行

@for /f "tokens=1 delims=[]" %%a in ('find /n "'Parent|Child"^<D:\DEV\Test\Cost_Center.txt') do @(
more +%%a D:\DEV\Test\Cost_Center.txt |find /v "!PropertyArray=Cost Center" || goto :eof)>D:\DEV\Test\Cost_Center_Out.txt

@for/f“tokens=1 delims=[]”%%a in('find/n“'Parent | Child“^您可以使用sed for Windows执行此操作:

sed "/'Parent|Child|IsPrimary|/,/!PropertyArray=Cost Center/!d" file1 > file2

这使用了一个名为
findrepl.bat
from-

术语开头的
^
表示它从第一列开始。您看到的
\\\\
\
字符的转义。

@ECHO OFF
@ECHO OFF
SETLOCAL 
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"
for /f "tokens=1 delims=[]" %%a in ('find /n "'Parent|Child"^<"%sourcedir%\Cost_Center.txt" ') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "!PropertyArray=Cost Center"^<"%sourcedir%\Cost_Center.txt" ') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%sourcedir%\Cost_Center.txt" ') do (
 IF %%a geq %start% IF %%a leq %end% ECHO(%%b
 )
)>"%destdir%\newfile.txt"
GOTO :EOF
SETLOCAL 设置“sourcedir=c:\sourcedir” 设置“destdir=c:\destdir”
对于('find/n“'Parent | Child”^中的/f“tokens=1 delims=[]”%%a是否还有以“or”开头的行?这些行号是否更改?行号内有引号?您说行“很长”是什么意思?谢谢。效果很好。但我得到了行“ECHO已关闭”在文件末尾。我修改了代码-更改非常微妙-
ECHO
变成
ECHO(
)。这将发生在空行上,其中未设置
%%b
,因此被解释为空字符串。谢谢。它可以工作。但它还包括行“!PropertyArray=Cost”.现在试试。我添加了一个筛选器以删除该行。
@ECHO OFF
SETLOCAL 
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"
for /f "tokens=1 delims=[]" %%a in ('find /n "'Parent|Child"^<"%sourcedir%\Cost_Center.txt" ') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "!PropertyArray=Cost Center"^<"%sourcedir%\Cost_Center.txt" ') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%sourcedir%\Cost_Center.txt" ') do (
 IF %%a geq %start% IF %%a leq %end% ECHO(%%b
 )
)>"%destdir%\newfile.txt"
GOTO :EOF