Page 1 of 1

Grab variable wihin a line of text that contains constants..

Posted: 21 Feb 2013 22:35
by pditty8811
I would like to use as the variable for my code below, instead of what came after ClassName=in 1.txt, I would like what came in-between this:

EntryText=Ship sunk!|Grid AO 77| Grab variable HERE, (notice the comma at the end)

So grab after the text line ending with the second | and before the comma.

The text line before the variable will be the same and constant EXCEPT after "Grid" there could be a word or a different two pair of letters and numbers, or any mixture of letters and numbers up until the |

So I am trying to use as a variable, what is in between:

EntryText=Ship sunk!|Grid (Any combination of letters or numbers)| (variable), (comma)

So grab in between the second | and the comma. Than you.


Code: Select all

@echo off

copy 2.txt 2.txt-backup

setlocal enableDelayedExpansion

>2.txt (
for /f "tokens=1* delims=:" %%A in ('findstr /n "^" 2.txt-backup') do (
    ( echo !ln!| findstr "^Type=206$" >NUL && set ln=ln ) || (
        set "ln=%%B"
        if "!ln:~0,6!"=="Class=" (
            findstr /c:"ClassName=!ln:~6!" "E:\Dropbox\New folder\Log_*.txt" >"E:\Dropbox\New folder\null" && (
                echo Class=ShipDummy
                set "ln=Type=206"
            )
        )
        if #!ln!==# (echo;) else echo !ln!
    )
  )
)



I know it has something to do with this, but I don't know how to apply it to the code above

Code: Select all

for /f "tokens=3 delims=|" %%C in ("%%B") do for /f "tokens=1 delims=," %%D in ("%%C") do echo %%D

Re: Grab variable wihin a line of text that contains constan

Posted: 22 Mar 2013 13:03
by !k

Code: Select all

set "str=EntryText=Ship sunk!|Grid AO 77| Grab variable HERE, (notice the comma at the end)"
setlocal enabledelayedexpansion
>my.txt echo:!str!
endlocal



for /f "usebackq tokens=1 delims=," %%a in (
   `for /f "tokens=3 delims=|" %%b in ^('findstr /c:"EntryText=" my.txt'^) do echo:%%b`
) do set "var=%%a"



echo var = "%var%"
pause

something like

Re: Grab variable wihin a line of text that contains constan

Posted: 22 Mar 2013 14:11
by pditty8811
This is old. I believe I've already solved this. Thank you.

Re: Grab variable wihin a line of text that contains constan

Posted: 22 Mar 2013 14:14
by Squashman
pditty8811 wrote:This is old. I believe I've already solved this. Thank you.

Then Please end your thread by saying it was solved and post what you solved your issue.

Re: Grab variable wihin a line of text that contains constan

Posted: 22 Mar 2013 14:30
by pditty8811
SOLVED
I'm sorry. I can't remember how it was solved. This was about a month ago.