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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

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

#1 Post by pditty8811 » 21 Feb 2013 22:35

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#2 Post by !k » 22 Mar 2013 13:03

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

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

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

#3 Post by pditty8811 » 22 Mar 2013 14:11

This is old. I believe I've already solved this. Thank you.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

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

#4 Post by Squashman » 22 Mar 2013 14:14

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.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

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

#5 Post by pditty8811 » 22 Mar 2013 14:30

SOLVED
I'm sorry. I can't remember how it was solved. This was about a month ago.

Post Reply