批处理脚本:在日志文件中搜索两个字符串并将值存储在变量中

时间:2015-04-24 11:37:01

标签: batch-file

要求: 我必须搜索文本"无法建立与远程服务器的连接。"在完整的日志文件中,以可变的逗号(,)分隔值获取"传出消息密钥的值,例如778445628,778439775,以便我可以使用可变值的值插入数据库表。

注意: 1)我不能直接搜索"传出消息密钥"因为消息键值也出现在另一个场景中,我想要"消息键"仅用于网络错误的值。 2)日志文件内容采用XML格式,因此XML标签位于日志文件中。

如果我在这里不清楚,请告诉我。 我必须在批处理脚本中实现解决方案。 请尽快协助。 提前谢谢。

请在下面找到我的示例输入日志文件内容:

Date Time: 2015-03-10 07:00:29

Server Name: abcde

Agent ID: 23

User Name: user

Message In: W6BFAssignmentEvents_OnAfterDelete event fired

Message Out:

Date Time: 2015-03-10 07:00:31

Server Name: abcde

Agent ID: 12

User Name: user

Error Number: -1

Error Description: <MessageResult Status="2"><Source>System</Source>
<Description>The connection to the remote server can not be established.</Description><Line>0</Line></MessageResult>

Error Source: W6IntUtilsLibGW.caer.ProcessPendingMessages

Outgoing Message Key: 778445628

Incoming Message:

Date Time: 2014-03-10 07:40:17

Server Name: abcde

Agent ID: 12

User Name: user

Error Number: -1

Error Description: <MessageResult Status="2"><Source>System</Source>

<Description>The connection to the remote server can not be established.</Description><Line>0</Line></MessageResult>

Error Source: W6IntUtilsLibGW.caer.ProcessPendingMessages

Outgoing Message Key: 778439775

Incoming Message:

1 个答案:

答案 0 :(得分:0)

我不喜欢用逗号分隔的键列表创建变量的想法,但是现在就去。

纯批次

@echo off
setlocal enableDelayedExpansion
set "noConnection="
set "keys="
for /f "tokens=1,4" %%A in (
  'findstr /r /c:"The connection to the remote server can not be established\." /c:"^Outgoing Message Key: " input.log'
) do (
  if %%A equ Outgoing (
    if defined noConnection set "keys=!keys!,%%B"
    set "noConnection="
  ) else set noConnection=1
)
set "keys=!keys:~1!"
echo !keys!


使用我的混合JScript /批量JREPL.BAT实用程序

@echo off
setlocal enableDelayedExpansion
set "keys="
for /f %%A in (
  'jrepl "The connection to the remote server can not be established\.[\s\S]*?^Outgoing Message Key: (\d+)" "$1" /m /jmatch /f input.log'
) do set "keys=!keys!,%%A"
set "keys=!keys:~1!"
echo !keys!

@echo off
setlocal
for /f %%A in (
  'jrepl "The connection to the remote server can not be established\.[\s\S]*?^Outgoing Message Key: (\d+)"^
         "keys+=','+$1;false" /m /jmatch /jbeg "var keys=''" /jend "output.WriteLine(keys.slice(1))" /f input.log'
) do set "keys=%%A"
echo %keys%