【问题标题】:Batch script: Search two strings in log file and store values in variable批处理脚本:在日志文件中搜索两个字符串并将值存储在变量中
【发布时间】:2015-04-24 21:52:57
【问题描述】:

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

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    我不喜欢用逗号分隔的键列表创建变量的想法,但你可以这样做。

    纯批处理

    @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/batch 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%
    

    【讨论】:

    • 非常感谢 dbenham 提供的“Pure Batch”解决方案,但我在运行此解决方案时遇到了一些语法错误。进一步的帮助将不胜感激。如果可能,请解释解决方案。
    • 您的建议和我的实现的唯一区别是,我使用的日志文件路径如下: for /f "tokens=1,4" %%A in ('findstr /r /c: "无法建立与远程服务器的连接\。" /c:"^Outgoing Message Key: " "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:"The connection to the remote无法建立服务器\。" /c:"^Outgoing Message Key: " "E:\MyFolder\Log Files\testlog.txt"') do (if %%A equ Outgoing (if defined noConnection set "keys=! keys!,%%B" 设置 "noConnection=" ) 否则设置 noConnection=1 ) 设置 "keys=!keys:~1!"回声%keys%
    猜你喜欢
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 2022-06-15
    • 2011-11-19
    相关资源
    最近更新 更多