【问题标题】:How to use a variable result in a match/select-string - Powershell如何在匹配/选择字符串中使用变量结果 - Powershell
【发布时间】:2020-09-18 15:19:53
【问题描述】:

我正在查看客户端持有应用程序会话的日志文件。 这没有清楚地记录,因此您必须首先找到正在更新的客户编号,然后再次检查为哪个客户分配了该编号的日志。

当我在匹配项中包含我找到的号码或选择字符串时,我无法完成这最后一步。写出来很简单。

这是我的过程:

    $path1 = "\\path\previous\logfile\.log.1"
    $path2 = "\\path\current\logfile\*.log"
    $getLogContent = Get-Content $path1, $path2
    $stringToSplit =$getlogcontent.where({$_ -match "Sending device updates to client"}, 'Last')
    $client= $stringtosplit.split("#")
    $clientString = $client[1]
    $number= $clientstring.split(":")
    $finalSearch = "got client ID $number"
    $SessionOwner= $getlogcontent | Select-String $finalSearch -Context 3,0
    $sessionOwner

这是我的数据:

PS:>$stringToSplit
2020-09-18 12:24:50,042 DEBUG P0000 [ApplicationSrv] [ApplicationFactory] Sending device updates to client #122:

我要匹配的对象:

PS:>$SessionOwner= $getlogcontent.where({$_ -match "got client ID"})
$SessionOwner
2020-09-18 06:54:56,914 INFO  P0000 Application[Client 115 ] registered successfully, got client ID 115.
2020-09-18 06:58:05,109 INFO  P0000 Application[Client 116 ] registered successfully, got client ID 116.
2020-09-18 07:00:04,013 INFO  P0000 Application[Client 117 ] registered successfully, got client ID 117.
2020-09-18 07:06:37,101 INFO  P0000 Application[Client 118 ] registered successfully, got client ID 118.
2020-09-18 07:06:43,081 INFO  P0000 Application[Client 119 ] registered successfully, got client ID 119.
2020-09-18 07:11:19,963 INFO  P0000 Application[Client 120 ] registered successfully, got client ID 120.
2020-09-18 10:20:26,638 INFO  P0000 Application[Client 121 ] registered successfully, got client ID 121.
2020-09-18 10:43:57,085 INFO  P0000 Application[Client 122 ] registered successfully, got client ID 122.
2020-09-18 10:59:21,873 INFO  P0000 Application[Client 123 ] registered successfully, got client ID 123.
2020-09-18 11:23:31,339 INFO  P0000 Application[Client 124 ] registered successfully, got client ID 124.
2020-09-18 11:41:13,406 INFO  P0000 Application[Client 125 ] registered successfully, got client ID 125.
2020-09-18 12:14:23,370 INFO  P0000 Application[Client 126 ] registered successfully, got client ID 126. 

我觉得我已经尝试了各种连接、正则表达式、"search text $varaible"'search text'$variable 并制作了一个新的完整 $variable。我完全不知道如何将$number 添加到我的字符串中并进行搜索。

【问题讨论】:

    标签: string-matching powershell-5.0


    【解决方案1】:

    它没有按您期望的方式工作的原因是因为这种分裂

    $number= $clientstring.split(":")
    

    实际输出两行

    $number | foreach {"Line $_"}
    Line 122
    Line
    

    你可以抓住第一个元素来纠正它。

    $number= $clientstring.split(":")[0]
    
    $number | foreach {"Line $_"}
    Line 122
    

    不过,我建议您稍微简化一下代码。这将一次性完成

    if($getLogContent.where({$_ -match '(?<=Sending device updates to client #)\d{3}'}, 'last'))
    {
        $number = $matches.0
    }
    
    $number | foreach {"Line $_"}
    Line 122
    

    您没有具体说明为什么要抓取上面的三行。然而,剩下的就是你的代码和我的建议

    $path1 = "\\path\previous\logfile\.log.1"
    $path2 = "\\path\current\logfile\*.log"
    $getLogContent = Get-Content $path1, $path2
    if($getLogContent.where({$_ -match '(?<=Sending device updates to client #)\d{3}'}, 'last'))
    {
        $number = $matches.0
    }
    $SessionOwner= $getlogcontent | Select-String "got client ID $number" -Context 3,0
    $sessionOwner
    

    哪些输出

      2020-09-18 07:06:43,081 INFO  P0000 Application[Client 119 ] registered successfully, got client ID 119.
      2020-09-18 07:11:19,963 INFO  P0000 Application[Client 120 ] registered successfully, got client ID 120.
      2020-09-18 10:20:26,638 INFO  P0000 Application[Client 121 ] registered successfully, got client ID 121.
    > 2020-09-18 10:43:57,085 INFO  P0000 Application[Client 122 ] registered successfully, got client ID 122.
    

    【讨论】:

    • 我有上下文的原因是,上面的第三行“获取客户端 ID”包含客户端正在为其申请 ID 的日志条目,为我们提供了会话持有者的身份。感谢您提供更优雅的方法和解决方案。
    • 很高兴听到它有帮助。如果它有助于解决问题并回答了您的问题,请考虑接受答案
    • 我只需要用 \d+ 替换 \d{3},因为这个数字是动态的,从 1 到他们自上次重置以来的登录次数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 2011-03-11
    • 1970-01-01
    • 2017-11-24
    相关资源
    最近更新 更多