Reputation: 171
I am looking to extract a specific word from a paragraph using DOS commands/script. I am setting the text to a variable.
set "Status=matched: 15:19:39 03/15/2016 from=10.18.20.116 oid=.1.3.6.1.4.1.1302.3.12.10.2.0.2 trap= n/a specific= n/a traptime=2822 hours 58 minutes 52 seconds community=NOM agent=10.18.20.116 version=v2c var1=1016273298 var2=.1.3.6.1.4.1.1302.3.12.10.2.0.2 var3=vmsitescope01 var4=1470979 Active Job Completed with Exit Status 58 var5=Alert Raised on: March 15, 2016 3:19 PM Job: 2726538 Tree Type : Server Tree Name : ALL MASTER SERVERS Nodes : fmsnbu700 Job Policy: FMS7-DD-Linux-ALL_LOCAL_DRIVES Exit Status: 58 (can't connect to client) Client: ipreavpada01nb New State: Done Alert Policy: Client Job Completion Status OpsCenter Server: FMSOPSCENTER01 Comment: var6=Client Job Completion Status var7= var8= var9=FMSOPSCENTER01"
i am trying to extract var9 value which is FMSOPSCENTER01.
I am trying to use '=' as a delimiter in a for statement and extracting second value for testing.
for /F "delims==" %G in ('%status%') DO (set "State=%H")
But its throwing a "Client: was unexpected at this time."
Some help would be appreciated.
-Abhi
Upvotes: 0
Views: 157
Reputation: 16266
Yes, I know this question is both old and already answered. Here is another way to do it. The apostrophe in the string had to be escaped with another apostrophe. This should not be a problem with data from a file or other stream.
@ECHO OFF
SET "Status=matched: 15:19:39 03/15/2016 from=10.18.20.116 oid=.1.3.6.1.4.1.1302.3.12.10.2.0.2 trap= n/a specific= n/a traptime=2822 hours 58 minutes 52 seconds community=NOM agent=10.18.20.116 version=v2c var1=1016273298 var2=.1.3.6.1.4.1.1302.3.12.10.2.0.2 var3=vmsitescope01 var4=1470979 Active Job Completed with Exit Status 58 var5=Alert Raised on: March 15, 2016 3:19 PM Job: 2726538 Tree Type : Server Tree Name : ALL MASTER SERVERS Nodes : fmsnbu700 Job Policy: FMS7-DD-Linux-ALL_LOCAL_DRIVES Exit Status: 58 (can''t connect to client) Client: ipreavpada01nb New State: Done Alert Policy: Client Job Completion Status OpsCenter Server: FMSOPSCENTER01 Comment: var6=Client Job Completion Status var7= var8= var9=FMSOPSCENTER01"
SET "var9="
SET "var5="
FOR /F %%a IN ('powershell -NoLogo -NoProfile -Command ^
" '%S%' | Where-Object { $_ -match 'var5=(.*) \w\w:' } | ForEach-Object { $Matches[1] } "') DO (SET "var5=%%a")
FOR /F %%a IN ('powershell -NoLogo -NoProfile -Command ^
" '%S%' | Where-Object { $_ -match '.*var9=(.*)\s*' } | ForEach-Object { $Matches[1] } "') DO (SET "var9=%%a")
ECHO var5 is %var5%
ECHO var9 is %var9%
Upvotes: 0
Reputation: 67296
@echo off
setlocal EnableDelayedExpansion
set "Status=matched: 15:19:39 03/15/2016 from=10.18.20.116 oid=.1.3.6.1.4.1.1302.3.12.10.2.0.2 trap= n/a specific= n/a traptime=2822 hours 58 minutes 52 seconds community=NOM agent=10.18.20.116 version=v2c var1=1016273298 var2=.1.3.6.1.4.1.1302.3.12.10.2.0.2 var3=vmsitescope01 var4=1470979 Active Job Completed with Exit Status 58 var5=Alert Raised on: March 15, 2016 3:19 PM Job: 2726538 Tree Type : Server Tree Name : ALL MASTER SERVERS Nodes : fmsnbu700 Job Policy: FMS7-DD-Linux-ALL_LOCAL_DRIVES Exit Status: 58 (can't connect to client) Client: ipreavpada01nb New State: Done Alert Policy: Client Job Completion Status OpsCenter Server: FMSOPSCENTER01 Comment: var6=Client Job Completion Status var7= var8= var9=FMSOPSCENTER01"
for %%a in (!Status!) do (
if "!var!" equ "var9" (
set "var9=%%a"
goto break
) else (
set "var=%%a"
)
)
:break
echo var9 = %var9%
In this method a plain for
command is used to separate all tokens in Status variable by space or equal-sign (or comma or semicolon, the default delimiters in the set of values of FOR command); then, just take the token after the "var9" one.
EDIT 2018/04/27: New method added
The new method below allows to get the value of all var#
variables:
@echo off
setlocal EnableDelayedExpansion
set "Status=matched: 15:19:39 03/15/2016 from=10.18.20.116 oid=.1.3.6.1.4.1.1302.3.12.10.2.0.2 trap= n/a specific= n/a traptime=2822 hours 58 minutes 52 seconds community=NOM agent=10.18.20.116 version=v2c var1=1016273298 var2=.1.3.6.1.4.1.1302.3.12.10.2.0.2 var3=vmsitescope01 var4=1470979 Active Job Completed with Exit Status 58 var5=Alert Raised on: March 15, 2016 3:19 PM Job: 2726538 Tree Type : Server Tree Name : ALL MASTER SERVERS Nodes : fmsnbu700 Job Policy: FMS7-DD-Linux-ALL_LOCAL_DRIVES Exit Status: 58 (can't connect to client) Client: ipreavpada01nb New State: Done Alert Policy: Client Job Completion Status OpsCenter Server: FMSOPSCENTER01 Comment: var6=Client Job Completion Status var7= var8= var9=FMSOPSCENTER01"
set "Status=%Status: var=" & set "var%"
echo var5="%var5%"
echo var9="%var9%"
Upvotes: 1
Reputation: 80211
@ECHO OFF
SETLOCAL
set "Status=matched: 15:19:39 03/15/2016 from=10.18.20.116 oid=.1.3.6.1.4.1.1302.3.12.10.2.0.2 trap= n/a specific= n/a traptime=2822 hours 58 minutes 52 seconds community=NOM agent=10.18.20.116 version=v2c var1=1016273298 var2=.1.3.6.1.4.1.1302.3.12.10.2.0.2 var3=vmsitescope01 var4=1470979 Active Job Completed with Exit Status 58 var5=Alert Raised on: March 15, 2016 3:19 PM Job: 2726538 Tree Type : Server Tree Name : ALL MASTER SERVERS Nodes : fmsnbu700 Job Policy: FMS7-DD-Linux-ALL_LOCAL_DRIVES Exit Status: 58 (can't connect to client) Client: ipreavpada01nb New State: Done Alert Policy: Client Job Completion Status OpsCenter Server: FMSOPSCENTER01 Comment: var6=Client Job Completion Status var7= var8= var9=FMSOPSCENTER01"
set "var9=%status:"=%"
set "var9=%status:&=%"
set "var9=%var9:* var9=%"
set "var9=%var9:~1%"
IF "%var9: =%" neq "%var9%" (
REM need to remove space and eveything after
FOR /L %%a IN (1,1,25) DO IF "!var9:~%%a,1!"==" " SET "var9=!var9:~0,%%a!"&GOTO done
)
:done
ECHO "%var9%"
set "var9=%status:"=%"
SET "var9="%var9: =" "%""
FOR %%a IN (%var9%) DO (
SET "varx=%%~a"
IF "!varx:~0,5!"=="var9=" SET "var9=!varx:~5!"&GOTO done2
)
:done2
ECHO "%var9%"
The first set
removes quotes from status
and assigns the result to var9
.
Then remove &
from var9
.
The next set
changes "anycharactersSpacevar9" to nothing
The next set
removes the =
(assuming the first "Spacevar9" is the critical string)
Next we test whether there is anything following the "var9=xxx". If so, then look for the space, grab the substring and forcefully terminate the loop. I've assumed that the string-to-be-retrived is <25 characters long.
Then there is a second routine that may suffice.
The first set
removes quotes from status
and assigns the result to var9
.
The next set
changes 'Space' to '"Space"' and puts quotes each end of the result. The consequence is that the string becomes a series of quoted-strings.
Next, process each of the quoted strings in turn. If the first 5 characters are 'var9=' then remove those first 5 and terminate the loop.
Note that this solution requires delayedexpansion
and may exhibit sensitivity to some symbols.
Upvotes: 0
Reputation: 56238
I suggest another method:
set "Status=matched: 15:19:39 03/15/2016 from=10.18.20.116 oid=.1.3.6.1.4.1.1302.3.12.10.2.0.2 trap= n/a specific= n/a traptime=2822 hours 58 minutes 52 seconds community=NOM agent=10.18.20.116 version=v2c var1=1016273298 var2=.1.3.6.1.4.1.1302.3.12.10.2.0.2 var3=vmsitescope01 var4=1470979 Active Job Completed with Exit Status 58 var5=Alert Raised on: March 15, 2016 3:19 PM Job: 2726538 Tree Type : Server Tree Name : ALL MASTER SERVERS Nodes : fmsnbu700 Job Policy: FMS7-DD-Linux-ALL_LOCAL_DRIVES Exit Status: 58 (can't connect to client) Client: ipreavpada01nb New State: Done Alert Policy: Client Job Completion Status OpsCenter Server: FMSOPSCENTER01 Comment: var6=Client Job Completion Status var7= var8= var9=FMSOPSCENTER01"
REM remove from beginning to (including) "Server: ":
set "status=%status:*Server: =%"
REM get the first token from the remaining string:
for /f %%a in ("%status%") do set "status=%%a
echo %status%
Upvotes: 0