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

Batch file 批处理脚本:在日志文件中搜索两个字符串并将值存储在变量中,batch-file,Batch File,要求: 我必须搜索文本,无法建立到远程服务器的连接。在完整的日志文件中,获取传出消息键的值,例如变量中的778445628、778439775作为逗号,分隔值,以便我可以使用变量的值插入到数据库表中 注: 1我无法直接搜索传出消息密钥,因为消息密钥值也出现在另一个场景中,并且我只希望消息密钥值用于网络错误。 2日志文件内容为XML格式,因此XML标记位于日志文件中 如果我不清楚,请告诉我。 我必须在批处理脚本中实现解决方案。 请尽快协助。 提前谢谢 请在下面找到我的示例输入日志文件内容: Dat

要求: 我必须搜索文本,无法建立到远程服务器的连接。在完整的日志文件中,获取传出消息键的值,例如变量中的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:

我不喜欢用逗号分隔的键列表创建一个变量,但现在开始吧

纯批次

使用我的混合JScript/批处理实用程序


非常感谢dbenham提供的纯批处理解决方案,但我知道在运行该解决方案时会出现语法错误。我们将非常感谢进一步的援助。如果可能,请解释解决方案。您的建议和我的实现中唯一的区别是,我使用的日志文件路径如下:for/f tokens=1,4%%A in'findstr/r/c:无法建立到远程服务器的连接\/c:^传出邮件密钥:E:\MyFolder\Log Files\testlog.txt“我正在Windows 2008上运行批处理脚本R2@Saurabh-我已经测试了所有三个代码,它们运行得非常好。注释中的代码末尾有一个额外的右括号。此外,do必须出现在同一行中,在一个正确的右括号之后。更正错误后,现在输出为~1。我在这里做错了什么:@echo off setlocal enableDelayedExpansion Set keys=Set noConnection=for/f tokens=1-4%%A in'findstr/I/c:无法建立到远程服务器的连接\/c:^传出消息键:E:\MyFolder\Log Files\testlog.txt'do if%%A eq outing if defined noConnection set keys=!钥匙!,%%B set noConnection=else set noConnection=1 set keys=!钥匙:~1!回显%keys%
@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!
@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%