我希望使用DOS命令/脚本从段落中提取特定单词。我正在将文本设置为变量。
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"
我正在尝试提取var9值,即FMSOPSCENTER01。
我正在尝试使用'='作为for语句中的分隔符并提取第二个值进行测试。
for /F "delims==" %G in ('%status%') DO (set "State=%H")
但它抛出一个“客户:此时出乎意料。”
一些帮助将不胜感激。
-Abhi
答案 0 :(得分:1)
@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"之后取得令牌。之一。
编辑2018/04/27 :添加新方法
下面的新方法允许获取所有var#
变量的值:
@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%"
答案 1 :(得分:0)
我建议另一种方法:
not null
答案 2 :(得分:0)
@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%"
第一个set
会移除status
的引号,并将结果分配给var9
。
然后从&
删除var9
。
下一个set
更改“任意字符 空间 var9”到没有
下一个set
删除=
(假设第一个“ Space var9”是关键字符串)
接下来,我们测试“var9 = xxx”后面是否有任何内容。如果是这样,那么寻找空间,抓住子串并强制终止循环。我假设要重试的字符串长度<25个字符。
然后还有第二个例程就足够了。
第一个set
会移除status
的引号,并将结果分配给var9
。
下一个set
更改'空间'到'“空间”'并在结果的每一端放置引号。结果是字符串变成了一系列带引号的字符串。
接下来,依次处理每个引用的字符串。如果前5个字符是'var9 =',则删除前5个字符并终止循环。
请注意,此解决方案需要delayedexpansion
,并且可能会对某些符号表现出敏感度。
答案 3 :(得分:0)
是的,我知道这个问题既老又已经回答了。这是另一种方法。字符串中的撇号必须与另一个撇号一起逃脱。对于来自文件或其他流的数据,这不应该是一个问题。
@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%