Link to home
Start Free TrialLog in
Avatar of pianomanJoe
pianomanJoeFlag for United States of America

asked on

parsing values from string in a windows batch file

Hi.  I am trying to write a windows batch script that parses a string of data to capture key elements.  The string contains both a token and the associated value.  The double carat (^^) separates the value and the double @@ separates the token/value pair.  For example with ACCESSKEY^^$P_ACCESSKEY, ACCESSKEY is the token, 123456 is the value.  @@ indicates a new token/value pair is beginning.  

I need a script that will parse the values from the scripts, i.e. the data between ^^ and @@.  Also, the tokens that begin with APP_PARM_ are optional, so I need to check if they exist, and if they do, parse the full APP_PARM_ token and value.  

Here is the data string that will be evaluated:
ACCESSKEY^^123456@@SECRETKEY^^98654321@@CONFIG_METHOD^^EBS_CONFIG@@APP_PARM_DBENDPOINT^^testdb@@APP_PARM_DBPASSWD^^access@@APP_PARM_DBUSER^^portalMaster@@APP_PARM_DBNAME^^DBTest

I'm not having any success with generating anything close.  So any help would be greatly appreciated!  Thanks,  Joe
Avatar of Bill Prew
Bill Prew

For the sample string, what results do you want returned?

~bp
Here's a starting point for some parsing in DOS, but not sure exactly how you want to build on this.  Hope it gives you some ideas, let me know what questions or enhancements you want.  The basic idea is to replace the ^^ and @@ with things that we can parse in the FOR statements, and then just work our way through it.

@echo off
set "Data=ACCESSKEY^^123456@@SECRETKEY^^98654321@@CONFIG_METHOD^^EBS_CONFIG@@APP_PARM_DBENDPOINT^^testdb@@APP_PARM_DBPASSWD^^access@@APP_PARM_DBUSER^^portalMaster@@APP_PARM_DBNAME^^DBTest"
set "Data=%Data:@@=,%"
set "Data=%Data:^^=:%"
for %%Z in (%Data%) do (
  for /F "tokens=1* delims=:" %%A in ("%%~Z") do (
    echo Token=%%~A, Value=%%~B
  )
)

Open in new window

~bp
Avatar of pianomanJoe

ASKER

Bill,  
Thanks for this.  It works nicely and the display is great!
Is it possible to set variables with a FOR statement?  Ultimately, I need to store the values parsed into a variable so I can reference it again.  I have not be able to set a variable in the DO portion.  I am probably going about it the wrong way.  
Yes, we can set variables in the FOR body.  What variable names would you want to use, would it be the actual Token name, or something else?

~bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bill, I really appreciate your help.  This is working great and I'm learning alot too.  That said, I've some across a problems related to this.  If you can help that would be great!

This is regarding the FOR statement that assign the variables.  I've just been informed that the variables passed in the string may vary by user/application.  The constant is that the token (before the ^^) must start with APP_PARM_.  My example had the tokens I was working with, but another could be APP_PARM_APPNAME or APP_PARM_VOLNAME, or others.   How would I parse the string, identify the tokens as identified in your script(SecretKey,AccessKey, ConfigMethod), but also to identify tokens that start with APP_PARM_ and still use the entire token string for the variable, i.e. APP_PARM_DBUSER as in my example).

Thanks!
Not sure I exactly understand.  Right now we are parsing the string like:

ACCESSKEY^^123456@@SECRETKEY^^98654321@@CONFIG_METHOD^^EBS_CONFIG@@APP_PARM_DBENDPOINT^^testdb@@APP_PARM_DBPASSWD^^access@@APP_PARM_DBUSER^^portalMaster@@APP_PARM_DBNAME^^DBTest

Open in new window

to get these variables assigned:

_var_ACCESSKEY=123456
_var_APP_PARM_DBENDPOINT=testdb
_var_APP_PARM_DBNAME=DBTest
_var_APP_PARM_DBPASSWD=access
_var_APP_PARM_DBUSER=portalMaster
_var_CONFIG_METHOD=EBS_CONFIG
_var_SECRETKEY=98654321

Open in new window

we certainly can identify variables that start with "APP_PARM_", but I don't understand what you want to do differently with them?

~bp
Here's an example of a change to recognize token names starting with that prefix, and display them, but not sure how you want to handle them differently than the others.

@echo off
setlocal EnableDelayedExpansion

set "Data=ACCESSKEY^^123456@@SECRETKEY^^98654321@@CONFIG_METHOD^^EBS_CONFIG@@APP_PARM_DBENDPOINT^^testdb@@APP_PARM_DBPASSWD^^access@@APP_PARM_DBUSER^^portalMaster@@APP_PARM_DBNAME^^DBTest"
set "Data=%Data:@@=,%"
set "Data=%Data:^^=:%"

for %%Z in (%Data%) do (
  for /F "tokens=1* delims=:" %%A in ("%%~Z") do (
    set Token=%%~A
    if "!Token:~0,9!" EQU "APP_PARM_" (
      echo Matching name found = [%%~A]
    )
    set "_var_%%~A=%%~B"
  )
)

echo.
echo ***** LIST ALL PARSED VARIABLES AND VALUES *****
set _var

echo.
echo ***** DISPLAY A SINGLE VARIABLES VALUE *****
echo %_var_ACCESSKEY%

endlocal

Open in new window

This produces this output:

Matching name found = [APP_PARM_DBENDPOINT]
Matching name found = [APP_PARM_DBPASSWD]
Matching name found = [APP_PARM_DBUSER]
Matching name found = [APP_PARM_DBNAME]

***** LIST ALL PARSED VARIABLES AND VALUES *****
_var_ACCESSKEY=123456
_var_APP_PARM_DBENDPOINT=testdb
_var_APP_PARM_DBNAME=DBTest
_var_APP_PARM_DBPASSWD=access
_var_APP_PARM_DBUSER=portalMaster
_var_CONFIG_METHOD=EBS_CONFIG
_var_SECRETKEY=98654321

***** DISPLAY A SINGLE VARIABLES VALUE *****
123456

Open in new window

~bp
PLEASE TRY THIS SIMPLE METHOD:


   @echo off
   set params="%~1"
   set params=%params:^==%
   set "params=%params:@@="&& set "%"
   set %params%


AND THAT'S ALL THERE IS TO IT !!


Save this code as TESTARG.BAT for example, and run it specifying your command line parameters:

   testarg ACCESSKEY^^123456@@SECRETKEY^^98654321@@CONFIG_METHOD^^EBS_CONFIG@@APP_PARM_DBENDPOINT^^testdb@@APP_PARM_DBPASSWD^^access@@APP_PARM_DBUSER^^portalMaster@@APP_PARM_DBNAME^^DBTest


You can confirm the variables are set correctly by including the following additional lines of code as shown below:

   @echo off
   set params="%~1"
   set params=%params:^==%
   set "params=%params:@@="&& set "%"
   set %params%

   echo.
   set ACCESSKEY
   set SECRETKEY
   set CONFIG_METHOD
   set APP_PARM_DBENDPOINT
   set APP_PARM_DBPASSWD
   set APP_PARM_DBUSER
   set APP_PARM_DBNAME



(Just fishing for a few points....)
Interesting treatment Paul.

~bp
Bill,
You're right, nothing needs to be changed.  Any token/value combos added to the string will be parsed.  Guess I just needed a fresh pair of eyes.  Thanks for your help.
@pianomanJoe

Glad you got something useful, thanks for the interaction, and grade.

~bp
pianomanJoe

I was certain I was going to be awarded SOME points for this one.

The command line parameter format is constant and therefore, my proposed method works for any number of variables.

You say you are learning a lot... Perhaps my method (whose logic may appear too obscure to you) is way too advanced for you even though in reality, it's far simpler to implement than billprew's solution (no disrespect to the old man).

I'm just wondering whether you took the trouble to try my code before totally dismissing it.
Paul,
Had already decided to use Billprew's suggestion since he was the only contributor until your post.  However, i didn't even see yours when I added my comment and closed the question.  When I did notice it, I tested it.  I'll sure I'll post in the future...
Apologies. It appears to be my fault then for not refreshing my browser's view before posting...