逐行解析批处理 [英] parse batch line by line

查看:91
本文介绍了逐行解析批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析另一个函数的输出,该函数逐行输出.要了解该功能,它将返回几行参数和数字,例如"top=123456789"或"low=123456789"(不带引号)-

i am trying to parse the output of another function which is output line by line. to understand the function, it returns several lines of parameter and numbers like "top=123456789" or "low=123456789" (without the quotations) -

我现在尝试用...来解析行

i try to parse the lines now with

for /F "delims=" %%a in ('%%I ^| findstr top') do set updir=%%1
set "updir=%1:~4%" 
echo. %updir%

我试图通过修剪已知关键字(例如top)来获取纯数字,然后将其设置为var以便将(%~1% ???)返回到调用函数(其他批处理文件).

i am trying to get the pure numbers by trimming the known keywords like top, which would need then to be set to a var to return (%~1% ???) to a calling function back (other batch file).

有人可以帮我吗?当然,最好从"="剪裁掉.

could anyone help me with this please? shure it would be better to trim right from "=".

更新:

这是从我链接的脚本返回行的代码.我尝试了几种方法来分析收益,但是我似乎是盲目的或太愚蠢而看不见,一切都变得很奇怪.

this is the code returning the lines from the script i linked. i tried several ways to parse the return but i seem to be blind or too stupid to see, all is going weird.

for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%URL%"') do (
rem process the HTML line-by-line

org echo(%%I

try1 (echo %%I|findstr top

try2 for /F "delims=" %%a in ('%%I ^| findstr top') do set updir=%%a
try2 echo. %updir%

try3 for /F "delims=" %%a in ('%%I') do findstr top
try3 echo. %2%


)

也不起作用

for /F "tokens=1,2delims==" %%a in ('%%I') do if %1 == top set updir=%%b
echo %updir%

我尝试了下面的两个delim版本(令牌/delims版本也是如此),但是我做错了.

i tried both delim version beneath (too the tokens/delims version) but i don't get it right.

更新解决方案:

对于在这里阅读该问题的人还有一些其他评论:

for the ones reading the question here some additional comment:

rem trim whitespace from beginning and end of line
for /f "tokens=*" %%x in ("%%~I") do set "line=%%x"

rem test that trimmed line matches "variable=number"

to find a single item like e.g. "top" you have to add "to" or adjust whole first token
回声!线! | findstr/i"^ to [a-z] = [0-9] "> NUL& (

to find a single item like e.g. "top" you have to add "to" or adjust whole first token
echo !line! | findstr /i "^to[a-z]=[0-9]" >NUL && (

rem test was successful.  Scrape number.
for /f "tokens=2 delims==" %%x in ("%%I") do set "value=%%x"
echo !value!
)

推荐答案

如果要做的就是跳过所有行,直到找到与"text = numerals"匹配的行,然后抓取该行的数字部分,您所需要做的就是这个:

If all you wish to do is to is to skip all lines until you find one that matches "text=numerals", then scrape the numeric portion of that line, all you need to do is this:

for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%URL%"') do (

    rem trim whitespace from beginning and end of line
    for /f "tokens=*" %%x in ("%%~I") do set "line=%%x"

    rem test that trimmed line matches "variable=number"
    echo !line! | findstr /i "^[a-z]*=[0-9]*$" >NUL && (

        rem test was successful.  Scrape number.
        for /f "tokens=2 delims==" %%x in ("%%I") do set "value=%%x"
    )
)

无论如何,我认为是对的.我没有测试.

I think that's right, anyway. I didn't test it.

但是我怀疑这将无法按预期工作,因为您要抓取的内容可能包含HTML标签.除非您粘贴示例页面的HTML源代码并解释要从该源代码示例中提取的内容,否则我们可能无法帮助您捕获HTML.

But I suspect that this is not going to work as you intend, since what you are scraping will probably include HTML tags. We will probably not be able to help you scrape the HTML unless you pastebin the HTML source of an example page, and explain what you wish to scrape from that source example.

这篇关于逐行解析批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆