0

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%

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 "=".

UPDATE:

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%


)

didn't work either

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

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

UPDATE SOLUTION:

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
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!
)
peet
  • 274
  • 3
  • 5
  • 18

3 Answers3

0

does this fit your needs?

for /F "delims=" %%a in ('type file.txt ^| findstr "top low"') do set /a %%a 

set top
set low
echo %top%, %low%
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • not at all, the used function does return single lines, i'd like to avoid writing first to a file are the vars top and low this way be given bacck to the calling script too? – peet Mar 19 '13 at 08:25
  • you might want to use the findstr with "/B" to compute only lines that actually START with "top" or "low". (You can set the searchstrings to "top= low=", so lines starting with e.g. "lowest=" will be ignored) – Stephan Mar 19 '13 at 08:28
  • "type" is only to show the function. You can also insert any function (or call) that gives text to the console. – Stephan Mar 19 '13 at 08:32
  • the function is a little bit to large to get it in the loop see here please [link]http://stackoverflow.com/questions/15395490/batch-script-get-html-site-and-parse-content - ah i guess i can use the call from there, i'll try – peet Mar 19 '13 at 08:35
0

Try this:

for /F "tokens=2delims==" %%a in ('findstr top file.txt') do set "updir=%%a"
echo.%updir%

According to your comment my new code:

@echo off &setlocal enabledelayedexpansion
set "string=%%I"
set "string=!string:*top=!"
for /f "delims== " %%z in ("!string!") do set "string=%%z"
echo !string!

.. output:

123456789

Edit2: "added.

Endoro
  • 37,015
  • 8
  • 50
  • 63
  • sorry did not see your answer because i scrolled down too much - what does the tokens definition mean? – peet Mar 19 '13 at 13:29
  • Token 1 = `top` and token 2 = `123456789`. Delimiter between the tokens is `=`. – Endoro Mar 19 '13 at 13:33
  • when i try to use %%I (which is return of delivering function) instead of file i get errors, can't figure it out how to parse the returned lines right – peet Mar 19 '13 at 22:09
  • What is the output from `echo(%%I` ? – Endoro Mar 19 '13 at 22:21
  • about 15 lines of strings like i noted above, most likewise "parameter=number.subnumber" – peet Mar 19 '13 at 22:23
  • Please show this output. You can also put it to http://pastebin.com/ and post the link here. – Endoro Mar 19 '13 at 22:27
  • parameter=4.2 parametera=12345 parab=abcde para_meter=1 par_ameter=1 top=123456789 low=123456.789 # bla bla bl(a)la bla end=987654321 – peet Mar 19 '13 at 22:53
  • when i use your code it return every false first token (chars) and the one right second token (number) – peet Mar 20 '13 at 21:25
  • thank you very much for your help, i shure don't wonna argue nor be unthankful, but if i put your second code in the for loop of the function it returns all strings like i said really, i shure did test it as i did not wonna waste your efforts and work. please try yourself – peet Mar 20 '13 at 22:13
  • your code does take all input from %%I but does only reset string right if top is in, if not it places the chars in right from token, so it returns the number of top and the not numbers of other %%I? – peet Mar 20 '13 at 22:18
  • your code does take all input from %%I but does only reset string right if top is in, if not it returns the not numbers of other %%I? – peet Mar 20 '13 at 22:24
  • or something like this, gettin sleepy sorry, but shure the string=string goes wrong – peet Mar 20 '13 at 22:26
0

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.

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.

Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
  • `do` what? Just kidding. I fixed it. Thanks mfm4aa! I wouldn't have minded if you had made the edit yourself, though. :) – rojo Mar 19 '13 at 23:49
  • Only two chars, minimum is five :) – Endoro Mar 19 '13 at 23:50
  • i will try it in a few minutes - i don't get html code, what i pasted above is the pure return i get from my page with only other chars and numbers – peet Mar 20 '13 at 06:05
  • sorry, took a while, awful day, BUT cool, when the $ is removed from the reg expression it works fine, thank you very much rojo – peet Mar 20 '13 at 21:19