1

Maybe you can help me guys, here's what I'm trying to do.

I have a text file which looks more or less like this:

something else

blah blah blah

Name: "value"; Param2: "value"; Param3: "value";

...

Name: "value"; Param3: "value"; Param4: "value"; Param5: "value";

more of something else

I'm looking for lines that start with "Name:" string and within that line I'll be interested in the value of one of those 'parameters' (every time I run it might be a different one). Every line might have a different amount of those parameters and might not contain the one I'm searching for.

So for example the parameter I'm looking for is 'Width:' and I need values from all lines that have that name. An example line I'm searching for could look like this

Name: "Test"; Range: "32"; Color: "#FFFFFF"; Width: "11"; Opacity: "0.1";

I need to get that Width value, which is 11.

So far I've managed to come up with this:

for /f "delims=" %%a in ('FIND "Name: " %1') do (
echo %%a
)

This finds every line with names and values withing the file (as mentioned before this file also contains some other stuff I don't need) and obviously outputs it. Now I have no idea how to find if there is a desired parameter and how to get it's value. I was thinking about searching for the param withing the line (from the example it would be Width: ", the cutting it out along with everything before it, then search for dual quotes and cutting them along with everything afterwords. This should leave just the desired value. I wasn't able to do it tho, hence I'm here asking for help.

Community
  • 1
  • 1

1 Answers1

1

Your use of FIND does not guarantee that the line starts with Name:.

The FINDSTR primitive regular expression capabilities can verify the line starts with Name:, as well as find the lines that contain the desired parameter.

Variable search and replace !var:*search=replace! can be used to remove all text from the beginning of the line through the desired parameter name.

An extra FOR /F can be used to parse out the first token of the remaining string.

for /f "delims=" %%A in (
  'findstr /rc:"^Name: .* Width: " %1'
) do (
  set "ln=%%A"
  setlocal enableDelayedExpansion
  for /f "delims=; " %%B in ("!ln:* Width: =!") do (
    endlocal
    echo %%~B
  )
)

There is one potential problem - the search and replace step could give the wrong result if an earlier parameter value has the desired parameter name embedded in a value. For example

Name: "value"; Comment: "I want the Width: value"; Width: "11"; etc...

Any utility that provides sophisticated regular expression search and replace capabilities could be used to provide an exact answer. For example, sed for Windows is freely available, and it could do the job.

I have written REPL.BAT - a hybrid JScript/batch utility that performs a regex search and replace on stdin and writes the result to stdout. It is pure script that will run natively on any modern Windows machine from XP onward. Full documentation is embedded within the script.

REPL.BAT can provide your answer in one step :) It should always work as long as the content follows the format you have layed out.

type %1 | repl "^Name: \q.*?\q;( [^:]+: \q.*?\q)* Width: \q(.*?)\q;.*" $2 ax

The regex could easily be modified to support varying amounts of spaces between tokens, if needed.

Community
  • 1
  • 1
dbenham
  • 119,153
  • 25
  • 226
  • 353