批处理-从变量中提取特定单词 [英] Batch - extract specific word from a variable
问题描述
我正在寻找使用DOS命令/脚本从段落中提取特定单词的方法.我正在将文本设置为变量.
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"
我正在尝试提取FMSOPSCENTER01的var9值.
i am trying to extract var9 value which is FMSOPSCENTER01.
我试图在for语句中使用'='作为分隔符,并提取第二个值进行测试.
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."
我们将不胜感激.
-阿比
推荐答案
@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%
在此方法中,普通的 for
命令用于按空格或等号(或逗号或分号,即FOR命令的值集中的默认定界符)分隔Status变量中的所有标记.;然后,只需将令牌放在"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.
编辑2018/04/27 :添加了新方法
下面的新方法允许获取所有 var#
变量的值:
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%"
这篇关于批处理-从变量中提取特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!