0

I have adopted some code from here:

Extract substring between 2 strings

Rem Looking for a test.txt substring preceded by page\[%%x\]= and succeeded by ;
@Echo Off

for /l %%x in (0, 1, 4) do (
   echo %%x
   For /F "Delims=" %%A In ('FindStr /I "page\[%%x\]=.*;" "test.txt"') Do Call :Sub "%%A" "%%x"
)

Pause
GoTo :EOF

:Sub
Set "Line=%~1"

Set "Up2Sub=%Line:*page[ %~2 ]=%"

Set "SubStr=%Up2Sub:;="&:"%"
Echo %SubStr% > test%~2.json
Exit /B

The issue I am having is regarding this line:

Set "Up2Sub=%Line:*page[ %~2 ]=%"

The variable %~2 does not get concatenated correctly.

What would be the syntax to get this value of the %~2 variable added to this SET statement in between the []?

Thanks

Sample text.txt

....

page[0]={*****};
page[1]={*****};
page[2]={*****};
page[3]={*****};
page[4]={*****};

....
khofm
  • 139
  • 4
  • 1
    Can you please add the example content of `test.txt` and fully explain what string and substring you're trying to capture from it. Also, you appear to be working with line content like `page[0]=*`, `page[1]=*`, `page[2]=*`, `page[3]=*` and `page[4]=*`, but then try to remove, all of the string up to `page[ 0 ]`, `page[ 1 ]`, `page[ 2 ]`, `page[ 3 ]`, and `page[ 4 ]`, which are clearly not there due to the spaces. – Compo Aug 29 '19 at 11:21
  • Thanks for the hint with the space. I removed them but it is still not working. I assume that the syntax for ```Set "Up2Sub=%Line:*page[%~2]=%"``` is incorrect. If I hardcode it like ```Set "Up2Sub=%Line:*page[0]=%"``` it's working properly. I have added a sample test.txt above. Thanks in advance! – khofm Aug 29 '19 at 11:27
  • you are correct: the `=` in `%line%` (respective `%~1` respective `%%A`) ruins the `set` syntax. Is your desired output (`{*******}`or just `*******`?). – Stephan Aug 29 '19 at 11:33
  • Desired output is {*******} - thanks Stephan – khofm Aug 29 '19 at 11:34

1 Answers1

1

To split a string, you can also use a for /f loop:

@Echo Off
for /l %%x in (0, 1, 4) do (
   For /F "Delims=" %%A In ('FindStr /I "page\[%%x\]=.*;" "test.txt"') Do (
     for /f "tokens=2 delims=;=" %%s in ("%%A") do echo %%x: %%s
   )
)
Pause

This splits the string (%%A) into tokens, separated by ; and =. With page[0]={*****};, the first token is page[0],then =is a delimiter. The second token is {*****} and the third token (after the;) is empty. we just include the ';' to the delimters to get rid of it.

With a little analyzing your file and choosing tokens and delims right, you can even reduce the code to a single for:

For /F "tokens=2,4 delims=[]{}" %%A In ('FindStr /I "page\[[0-4]\]=.*;" "test.txt"') Do echo {%%B}>test%%A.json
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Something is wrong. Using the first approach leads to unreliable results. Sometimes the certain occurrences are not being picked up. – khofm Aug 30 '19 at 11:00
  • Seems to vary by input file. Even though the format is exactly the same. Any chance this has to do with any hidden encoding or EOL or something like that? Happy to share a sample test.txt that leads to unreliable results. – khofm Aug 30 '19 at 11:14
  • I think it has to do with the overall length of the string within {*****}. https://stackoverflow.com/questions/8844868/what-are-the-undocumented-features-and-limitations-of-the-windows-findstr-comman the Line Length Limit seems to be the reason. Any ideas how to prevent this? @Stephan – khofm Aug 30 '19 at 13:22
  • Line length shouldn't be a problem. Dbenham says `Files specified as a command line argument or via the /F:FILE option have no known line length limit.`. I learned to trust him on such statements. (there *is* a limit on the search string, but that doesn't apply here). Line endiings (there are `LF`, `CR` and `CRLF`) might matter, as well as file encoding. Also any special chars within the line could make problems. – Stephan Aug 30 '19 at 14:55
  • This is a file that will generate such a partial fail: https://pastebin.com/Z5fbZzyj (only some occurances of ```page[n]``` are found.) – khofm Aug 30 '19 at 16:30
  • uh - there are a lot of [, ], { and } in the lines which mess with our delims. `findstr` has no problem with the lne length, but I guess the `for` variables (`%%A`) might have a limitation. With such data, batch isn't the right choice. Google for `jrepl.bat`, which has full REGEX support and can do REGEX substitution. – Stephan Aug 30 '19 at 20:53