How to parse and extract data from string with jrepl

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mauriziotarducci
Posts: 3
Joined: 19 Oct 2022 09:38

How to parse and extract data from string with jrepl

#1 Post by mauriziotarducci » 19 Oct 2022 09:52

How to parse and extract data from string with jrepl .
I have a string like :

000006,1666026219,"2022/10/17","19:03:39","172.24.48.245","XYZ","audit","info","Oct 17 08:18:49 SND1 Group: CC224010 - POE: SND10020 - UserID: MVSMXT - CmdUserID: MVSMXT6

i would like to extract the value after some of the word like UserID, Group,POE
and have in variable the value after the above words .

so obtain variables that could be :

%UserID% = MVSMXT
%POE% = SND10020
...

could be possible using jrepl ? or a standard way into Windows Batch script ?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to parse and extract data from string with jrepl

#2 Post by aGerman » 19 Oct 2022 17:10

Assuming your string is read from a file (test.txt in the example below), JREPL could be used like that:

Code: Select all

@echo off
for /f "delims=" %%i in (
 'jrepl.bat ".*- POE: ([^-\s]+).*- UserID: ([^-\s]+).*" "set \x22POE=$1\x22\nset \x22UserID=$2\x22" /XSEQ /A /F "test.txt"'
) do %%i

echo %POE%
echo %UserID%
pause
Steffen

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to parse and extract data from string with jrepl

#3 Post by Aacini » 19 Oct 2022 18:25

This is a very simple standard Windows Batch method:

Code: Select all

@echo off
setlocal

set "string=000006,1666026219,"2022/10/17","19:03:39","172.24.48.245","XYZ","audit","info","Oct 17 08:18:49" SND1 Group: CC224010 - POE: SND10020 - UserID: MVSMXT - CmdUserID: MVSMXT6"

set "x=%string:SND1 =" & set "x=%"
set "x=%x:: ==%"
set "%x: - =" & set "%"

echo Group=%Group%
echo POE=%POE%
echo UserID=%UserID%
echo CmdUserID=%CmdUserID%
Output:

Code: Select all

Group=CC224010
POE=SND10020
UserID=MVSMXT
CmdUserID=MVSMXT6
NOTE: I assumed you missed a quote before "SND1" word, so I inserted it. If the data really have an odd number of quotes, then a small adjustment is required in the code.

If you want to know where the magic is, remove the @echo on line, run the Batch file and carefully review the output...

Antonio

mauriziotarducci
Posts: 3
Joined: 19 Oct 2022 09:38

Re: How to parse and extract data from string with jrepl

#4 Post by mauriziotarducci » 20 Oct 2022 06:20

Hi Antonio

very much thanks to your effort :

in case the string is more complicated and contains some other info in the between :

set "string=000001,1666026232,"2022/10/17","19:03:52","172.24.48.245","mvssnd1.bmc.com","audit","info","Oct 17 08:18:49 SND1 RACF: ADDUSER: Auth: SPECIAL - Group: CC224010 - JobNm: MVSMXT - Owner: MVSMXT - Name: XYX - Reas_SETROPTS: Yes - Reas_Special: Yes - Reas_User: Yes - RdrTime: 2022-10-14T02:11:59.790 - POE: SND10020 - POEclass: Terminal - SessType: TSO Logon - TermNm: SND10020 - UserID: MVSMXT - KeywdSpec: {MODEL, NAME} - CmdUserID: MVSMXT6

and i want to extract Userid , Group, POE and CmdUserid

thanks in advance again a great thanks

Maurizio

mauriziotarducci
Posts: 3
Joined: 19 Oct 2022 09:38

Re: How to parse and extract data from string with jrepl

#5 Post by mauriziotarducci » 20 Oct 2022 06:24

Hi Steffen

first very much thanks .

in your example you read a file

@echo off
for /f "delims=" %%i in (
'jrepl.bat ".*- POE: ([^-\s]+).*- UserID: ([^-\s]+).*" "set \x22POE=$1\x22\nset \x22UserID=$2\x22" /XSEQ /A /F "test.txt"'
) do %%i

echo %POE%
echo %UserID%
pause

could i use jrepl to read from a variable %myvar% ?

thanks in advance
Maurizio

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to parse and extract data from string with jrepl

#6 Post by aGerman » 20 Oct 2022 09:51

Like Antonio, I also assume that a quotation mark is missing somewhere because they are unbalanced. Unbalanced quotes lead to errors, so I added one.

Code: Select all

@echo off
set "myvar=000006,1666026219,"2022/10/17","19:03:39","172.24.48.245","XYZ","audit","info","Oct 17 08:18:49 SND1 Group: CC224010 - POE: SND10020 - UserID: MVSMXT - CmdUserID: MVSMXT6""

for /f "delims=" %%i in (
 'echo %myvar%^|jrepl.bat ".*- POE: ([^-\s]+).*- UserID: ([^-\s]+).*" "set \x22POE=$1\x22\nset \x22UserID=$2\x22" /XSEQ /A'
) do %%i

echo %POE%
echo %UserID%
pause

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to parse and extract data from string with jrepl

#7 Post by Aacini » 20 Oct 2022 09:53

mauriziotarducci wrote:
20 Oct 2022 06:20
Hi Antonio

very much thanks to your effort :

in case the string is more complicated and contains some other info in the between :

set "string=000001,1666026232,"2022/10/17","19:03:52","172.24.48.245","mvssnd1.bmc.com","audit","info","Oct 17 08:18:49 SND1 RACF: ADDUSER: Auth: SPECIAL - Group: CC224010 - JobNm: MVSMXT - Owner: MVSMXT - Name: XYX - Reas_SETROPTS: Yes - Reas_Special: Yes - Reas_User: Yes - RdrTime: 2022-10-14T02:11:59.790 - POE: SND10020 - POEclass: Terminal - SessType: TSO Logon - TermNm: SND10020 - UserID: MVSMXT - KeywdSpec: {MODEL, NAME} - CmdUserID: MVSMXT6

and i want to extract Userid , Group, POE and CmdUserid

thanks in advance again a great thanks

Maurizio

At top of this forum there is a link to this post. However, it seems you didn't saw it, so I copied here the relevants parts of such a post for you:
foxidrive wrote:
08 Dec 2014 05:51
The fastest way to get code that is robust and efficient is to clearly describe what you need to do and then let the programmers decide the best way to handle the job.

Batch files are often specific to your task because your filepaths, text, filenames and data are often used to write code that is both simpler and more efficient.

It makes it easy for people if you provide accurate details. The script you are given is designed to work in your specific situation and they will often add extra code to handle aspects that you may not have thought of.

If you hide your details by using fake names and fake paths and fake file information then nobody knows if you are using things like non-latin characters, or unicode, or poison characters for batch scripts. In these situations the volunteers can't include appropriate code to handle your special situations because they aren't aware you need it.

By providing poor information about your task the code you are given stands a good chance of failing.

Batch code is often written by analysing the characters and the patterns in text/numbers/paths/filenames that are being handled by the batch script. Wrong analysis, when all you have is wrong data, just leads to wrong batch code.

The layout of your text, numbers, paths, and filenames is also important when writing code so type over the sensitive details in your examples to disguise them but don't change the layout or length of these lines.

Please show respect to those people who give their free time to provide you with code by giving them accurate information about your task.

Come back later and tell them if the code helped you, and saying thank you after you receive some help doesn't cause hairy palms or bad luck so give that a shot too.

You didn't answered the question about if your data contain an odd number of quotes, so I added a quote anyway...

Code: Select all

@echo off
setlocal

set "string=000001,1666026232,"2022/10/17","19:03:52","172.24.48.245","mvssnd1.bmc.com","audit","info","Oct 17 08:18:49 SND1 RACF: ADDUSER: Auth: SPECIAL - Group: CC224010 - JobNm: MVSMXT - Owner: MVSMXT - Name: XYX - Reas_SETROPTS: Yes - Reas_Special: Yes - Reas_User: Yes - RdrTime: 2022-10-14T02:11:59.790 - POE: SND10020 - POEclass: Terminal - SessType: TSO Logon - TermNm: SND10020 - UserID: MVSMXT - KeywdSpec: {MODEL, NAME} - CmdUserID: MVSMXT6"

set x=%string:ADDUSER: =" & set "x=%"
set "x=%x:: ==%"
set "%x: - =" & set "%"

echo Group=%Group%
echo POE=%POE%
echo UserID=%UserID%
echo CmdUserID=%CmdUserID%
Output:

Code: Select all

Group=CC224010
POE=SND10020
UserID=MVSMXT
CmdUserID=MVSMXT6
You can get any value placed after ADDUSER:.

Antonio

Post Reply