0

So I am looking to run a batch file which would extract the value of a particular variable from a string. For Example:

[2017-11-03T12:18:56.263733+00:00] [XFM] [TRACE:1] [EPMHFM-00000] [XFM] [ecid: XDS.0000.0000.0000.0001] [File: CHsvDSSystemInfo.cpp] [Line: 6002] [userId: ] [appName: GGCOST] [pid: 9640] [tid: 6632] [host: AWEU1ORHYAP05P] [nwaddr: 172.20.36.105:0;] [errorCode: 0] [srcException: NotSpec] [errType: 1] [dbUpdate: 1] [11.1.2.4.202.5211]  [[SYSINFO:Application=GGCOST; PID=9640; NumUsers=0; CPU(Cores)=8; NumTasks=0; NumErrors=1; PhysicalMem=60129071104; UsedPhysicalMem=38635540480; ProcUsedPhysicalMem=3277619200; VirtualMem=140737488224256; UsedVirtualMem=5590806528; ProcUsedVirtualMem=3582775296; UsedCPU=9.3284; ProcUsedCPU=56.3594; NumCubesInRAM=7007; NumDataRecordsInRAM=1693604; NumRecordsInLargestCube=92334]]  

From the above string, I am looking to extract the pid number, which in this case is 6212. Can it be done via a batch script?

2 Answers2

1
@ECHO OFF
SETLOCAL
SET "var=[2017-11-11T07:27:23.088173+00:00] [XFM] [TRACE:1] [EPMHFM-00000] [XFM] [ecid: ] [File: XFMDataSource.cpp] [Line: 508] [userId: ] [Msg arguments: ] [appName: MyApp] [pid: 6212] [tid: 3176] [host: Server1] [nwaddr: 172.24.62.192:0;] [errorCode: 0] [srcException: NotSpec] [errType: 1] [[XDS: XFMDataSource process exiting now ...]] "
SET "var1=%var:(= %"
SET "var1=%var1:)= %"
SET "result=not found "
FOR %%a IN (%var1%) DO IF "%%a"=="[pid:" (
  SET "result="
 ) ELSE ( 
  IF NOT defined result SET result=%%a
 )
SET "result=%result:~0,-1%"

ECHO result is %result%

GOTO :EOF

So yes, it's possible. This is one of many ways.

first set result to a report-string, then use the standard for functionality on the string var. When the predecessor string [pid: is found, set result to nothing so that when the for delivers the next string to %%a, result will be undefined and %%a will not be [pid: - then set result to the string found (which means result is now defined)

After processing the remaining strings, all that is required is to remove the final character from result.

Another approach would be

FOR %%a IN (%var:]=%) DO IF "%%a"=="[pid:" (

which would replace the ] characters with nothing which would mean that the value set as result would not require the removal of the last character so the set following the for loop would not be required.

I've inserted two lines to remove the ( and ) which would cause a problem with the new data you've posted. Note that a new variable, var1 is derived and processed.

There are a number of characters which cause cmd problems and require special processing. ! will cause problems if delayedexpansion is invoked, but we need to know this type of information before devising a solution as it affects the code required.

The code will find the string following last appearance of the string [pid: in the variable. The string following PID will be ignored.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks it did work for that particular example. However, I guess the example I gave was not robust enough. I have updated the example since and it doesn't appear to work for it, though it should since I believe the for loop only takes into account the first pid value correct? Also I have set enabledelayedexpansion earlier, does it effect the code? – Debojit Bhowmick Nov 11 '17 at 10:11
  • I tried several tests on the code and it appears that %%a is taking in the entire line in the first iteration rather than only picking up until the space. Any idea why? – Debojit Bhowmick Nov 11 '17 at 11:13
  • I can't see your tests, but I'd conclude that your variable is "quoted" – Magoo Nov 11 '17 at 12:29
  • Indeed you are right. But if I don't put the quote, it says "8 was unexpected" – Debojit Bhowmick Nov 11 '17 at 12:50
  • However thanks. The updated quote now works perfectly! – Debojit Bhowmick Nov 11 '17 at 12:53
  • Ah, yes - but the `8 is unexpected` is symptomatic of a `)` within the string being processed. `cmd` interprets this as the end of the `(list)` hence since it's not wanted, processing to eliminate it. `(` processed similarly to keep the universe in balance. – Magoo Nov 11 '17 at 13:14
1

Another method, I learned just yesterday

@echo off

>t.txt echo [2017-11-03T12:18:56.263733+00:00] [XFM] [TRACE:1] [EPMHFM-00000] [XFM] [ecid: XDS.0000.0000.0000.0001] [File: CHsvDSSystemInfo.cpp] [Line: 6002] [userId: ] [appName: GGCOST] [pid: 9640] [tid: 6632] [host: AWEU1ORHYAP05P] [nwaddr: 172.20.36.105:0;] [errorCode: 0] [srcException: NotSpec] [errType: 1] [dbUpdate: 1] [11.1.2.4.202.5211]  [[SYSINFO:Application=GGCOST; PID=9640; NumUsers=0; CPU(Cores)=8; NumTasks=0; NumErrors=1; PhysicalMem=60129071104; UsedPhysicalMem=38635540480; ProcUsedPhysicalMem=3277619200; VirtualMem=140737488224256; UsedVirtualMem=5590806528; ProcUsedVirtualMem=3582775296; UsedCPU=9.3284; ProcUsedCPU=56.3594; NumCubesInRAM=7007; NumDataRecordsInRAM=1693604; NumRecordsInLargestCube=92334]]

for /F "tokens=3" %%a in ('"setx /F t.txt /D ] dummyVar /R 0,1 [pid:"') do set "var=%%a" & goto continue
:continue
set "var=%var:~0,-1%"
echo %var%

Cons: needs a file; creates a permanent environment variable (which of course can easily be deleted)

Stephan
  • 53,940
  • 10
  • 58
  • 91