Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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 Findstr以输出子级和父级_Batch File_Command Line_Cmd_Command Prompt_Findstr - Fatal编程技术网

Batch file Findstr以输出子级和父级

Batch file Findstr以输出子级和父级,batch-file,command-line,cmd,command-prompt,findstr,Batch File,Command Line,Cmd,Command Prompt,Findstr,我正在尝试使用命令行组织xml/kml文件。我可以使用findstr“STRING”file.txt查找我只需要的数据,但似乎无法从其父级获取其余的子级数据。kml文件的结构如下 <Placemark> <name></name> <description> [The sring data I need] </description> <Point><coordinates></coordinates>

我正在尝试使用命令行组织xml/kml文件。我可以使用
findstr“STRING”file.txt
查找我只需要的数据,但似乎无法从其父级获取其余的子级数据。kml文件的结构如下

<Placemark>
<name></name>
<description> [The sring data I need] </description>
<Point><coordinates></coordinates></Point>
</Placemark>

[我需要的sring数据]
当我运行
findstr
时,我只获取描述数据,需要获取以上所有内容,有什么想法吗?

grep-A3-B2“String”file.txt适合我 谢谢@zb226

grep-A3-B2“String”file.txt为我工作 谢谢@zb226

还有,纯批次

set "init="
set "term="
for /F "tokens=1,* delims=[]" %%A in ('type yourFile.xml ^| find /I /N "placemark"') do (
  if not defined init (set /a init=%%A) else (set /a term=%%A)
)
for /F "tokens=1,* delims=[]" %%A in ('type yourFile.xml ^| find /N /V "^"') do (
  if %%A GEQ %init% if %%A LEQ %term% echo/%%B
)
编辑:问题是('type yourFile.xml…

写到文件中

set "init="
set "term="
for /F "tokens=1,* delims=[]" %%A in ('type yourFile.xml ^| find /I /N "placemark"') do (
  if not defined init (set /a init=%%A) else (set /a term=%%A)
)
>"myFile.txt" (
  for /F "tokens=1,* delims=[]" %%A in ('type yourFile.xml ^| find /N /V "^"') do (
    if %%A GEQ %init% if %%A LEQ %term% echo/%%B
  )
)
因此,任何
echo…
都会打印到myFile.txt中,而且是纯批处理

set "init="
set "term="
for /F "tokens=1,* delims=[]" %%A in ('type yourFile.xml ^| find /I /N "placemark"') do (
  if not defined init (set /a init=%%A) else (set /a term=%%A)
)
for /F "tokens=1,* delims=[]" %%A in ('type yourFile.xml ^| find /N /V "^"') do (
  if %%A GEQ %init% if %%A LEQ %term% echo/%%B
)
编辑:问题是('type yourFile.xml…

以及写入文件

set "init="
set "term="
for /F "tokens=1,* delims=[]" %%A in ('type yourFile.xml ^| find /I /N "placemark"') do (
  if not defined init (set /a init=%%A) else (set /a term=%%A)
)
>"myFile.txt" (
  for /F "tokens=1,* delims=[]" %%A in ('type yourFile.xml ^| find /N /V "^"') do (
    if %%A GEQ %init% if %%A LEQ %term% echo/%%B
  )
)

因此,任何
echo…
都会打印到myFile.txt

中,我肯定会建议您在可用时使用
grep
解决方案。 因为我很想看看如何用批处理文件脚本解决这个问题,所以问题被标记为
batch file
,毕竟,为了完整起见,我还是决定发布脚本

请记住,在使用批处理文件执行字符串搜索时,始终存在一些限制/特殊情况

脚本将显示
lines
变量指定的行数。
offset
变量指定字符串应在哪一行上查找。当找到多个匹配项时,仅显示最后一个匹配项

@echo off

setlocal enabledelayedexpansion
set "source=file.txt"
set "find=[The string data I need]"
set "lines=5"
set "offset=3"

for /f "delims=:" %%e in ('findstr /n /c:"%find%" "%source%"') do (
  set /a position=%%e-offset
)

if not defined position (
  echo No matches found for: %find%
  exit /b
)

for /f "usebackq skip=%position% delims=" %%e in ("%source%") do (
  if !count!0 lss %lines%0 (
    echo %%e
    set /a count+=1
  )
)

我绝对建议您在可用时选择
grep
解决方案。 因为我很想看看如何用批处理文件脚本解决这个问题,所以问题被标记为
batch file
,毕竟,为了完整起见,我还是决定发布脚本

请记住,在使用批处理文件执行字符串搜索时,始终存在一些限制/特殊情况

脚本将显示
lines
变量指定的行数。
offset
变量指定字符串应在哪一行上查找。当找到多个匹配项时,仅显示最后一个匹配项

@echo off

setlocal enabledelayedexpansion
set "source=file.txt"
set "find=[The string data I need]"
set "lines=5"
set "offset=3"

for /f "delims=:" %%e in ('findstr /n /c:"%find%" "%source%"') do (
  set /a position=%%e-offset
)

if not defined position (
  echo No matches found for: %find%
  exit /b
)

for /f "usebackq skip=%position% delims=" %%e in ("%source%") do (
  if !count!0 lss %lines%0 (
    echo %%e
    set /a count+=1
  )
)

虽然可能需要付出很多努力,但我强烈建议使用标准*nix
grep
的Windows端口,使用该端口,解决方案可以归结为
grep-A2-B2“STRING”file.txt
。明白了。虽然可能需要付出很多努力,但我强烈建议使用标准*nix
grep
的Windows端口,使用该端口,解决方案可以归结为
grep-A2-B2“STRING”file.txt
。获取它。我想这可能有语法错误,或者需要转义字符。对我不起作用。我应该在哪里写入文件,以便查看它是否工作,因为它对我没有任何作用。我想这可能有语法错误,或者需要转义字符。对我不起作用。我应该在哪里写入文件,以便查看它是否工作因为它没有为我做任何事情,任何一个。它只是关闭,在“查找”下,我需要将我的字符串放入[]中吗?或就在=?之后,将需要查找的字符串放在等号之后。就我个人而言,我发现从现有命令提示符窗口执行脚本更方便,因为您只需按向上箭头键即可再次执行。如果您喜欢从窗口资源管理器执行脚本,可以在文件结尾,在任何
退出
命令之前。它只是关闭,在“查找”下,我是否需要将字符串放入[]中?或就在=?之后,将需要查找的字符串放在等号之后。就我个人而言,我发现从现有命令提示符窗口执行脚本更方便,因为您只需按向上箭头键即可再次执行。如果您喜欢从窗口资源管理器执行脚本,可以在文件结尾,在任何
退出
命令之前。