2017-06-09 30 views
0

我想用命令行组织我的xml/kml文件。我可以使用findstr "STRING" file.txt来查找我只需要的数据,但似乎无法从其父项中获取剩余的子项。 KML文件的结构类似于Findstr输出孩子和父母

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

当我运行findstr我只得到描述数据和需要得到上述所有,任何想法?

+1

虽然也许可以用了很大的力气,我强烈建议使用标准的Windows移植* nix中'grep',与该解决方案可以归结为'grep的-A2-B2“STRING”文件。 txt'。得到它[这里](http://gnuwin32.sourceforge.net/packages/grep.htm)。 – zb226

回答

1

的grep-A3-B2 “字符串” file.txt的工作对我来说 感谢@ zb226

1

此外,纯一批

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前报价线for /F "tokens=1,* delims=[]" %%A in ('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

+0

我认为这可能有语法错误,或需要转义字符。没有为我工作。 – lnsflive

+0

我在哪里写入文件,所以我可以看到它的工作原理,因为它对我没有任何帮助。 – lnsflive

+0

@lnsflive代码编辑,我犯了一个错字。希望它可以帮助 – elzooilogico

1

我肯定会建议你去为grep解决方案时可用。 因为我有兴趣了解如何通过批处理文件脚本解决问题—为什么—为了完整起见,我决定发布脚本。

请记住,使用批处理文件执行字符串搜索时总会遇到一些限制/特殊情况。

该脚本将显示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 
) 
) 
+0

它只是关闭,找到我需要把我的字符串内的[]?或者在=之后? – lnsflive

+0

在等号后面放置需要查找的字符串。就我个人而言,我发现从现有的命令提示符窗口执行脚本更方便,因为您只需按下向上箭头键即可再次执行它。如果您更喜欢从Window Explorer执行脚本,则可以在文件的末尾和任何'exit'命令之前放置'pause'命令。 – treintje