【问题标题】:Select-string regex选择字符串正则表达式
【发布时间】:2021-05-16 06:03:45
【问题描述】:

我正在使用 foreach 循环搜索大量日志以查找字符串 ($text),并且当前将整行输出到输出文件 ($logfile)

Get-ChildItem "\\$server\$Path" -Filter "*.log" |select-string -pattern $text |select -expandproperty line |out-file $logfile -append

其中一个日志文件的示例行可能如下所示

May 25 04:08:36.640 2016 AUDITOF GUID 1312.2657.11075.54819.13021094807.198 opened by USER

在哪里$text = "opened by USER"

所有这一切都很好,它会吐出每个包含 $text 的日志文件的每一行,这很棒。

但是.. 我想我想做的是获取日期时间和 GUID 的输出。 Guid 可以更改格式、长度等,但它总是有点,并且总是在 GUID (space) 之后和 (space) opened 之前

简而言之,我正在尝试使用后向(或前向)或匹配来进行正则表达式,以将类似的内容返回到 $logfile

2016 年 5 月 25 日 04:08:36.640,1312.2657.11075.54819.13021094807.198

任何帮助表示赞赏。我不喜欢正则表达式。

【问题讨论】:

    标签: regex powershell regex-lookarounds select-string


    【解决方案1】:

    一种方法是这样做

    $result = Get-ChildItem "\\$server\$Path" -Filter "*.log" -File | 
              Select-String -Pattern $text -SimpleMatch |
              Select-Object -ExpandProperty Line |
              ForEach-Object {
                  if ($_ -match '([a-z]{3,}\s*\d{2}\s*\d{2}:\d{2}:\d{2}\.\d{3}\s*\d{4}).*GUID ([\d.]+)') {
                      '{0},{1}' -f $matches[1], $matches[2]
                  }
              }
    
    $result | Out-File $logfile -Append 
    

    解释:

    • 我在Select-String cmdlet 中添加了开关-SimpleMatch,因为您似乎想完全匹配$text,而且它在那里不使用正则表达式,这将是最佳选择。
    • Select-Object -ExpandProperty Line 可以返回一个匹配行的数组,所以我通过管道将它传递给 ForEach-Object 以循环
    • if (..) 使用正则表达式 -match,如果该条件为 $true,我们将执行大括号内的任何操作。
      此外,此测试(如果为 $true)会自动设置一个 $matches 对象数组,我们使用这些匹配项输出一个逗号分隔的行,然后将其收集到变量 $result 中。
    • 最后我们简单地将 $result 输出到一个文件中

    正则表达式详细信息:

    (               Match the regular expression below and capture its match into backreference number 1
       [a-z]        Match a single character in the range between “a” and “z”
          {3,}      Between 3 and unlimited times, as many times as possible, giving back as needed (greedy)
       \s           Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
          *         Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
       \d           Match a single digit 0..9
          {2}       Exactly 2 times
       \s           Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
          *         Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
       \d           Match a single digit 0..9
          {2}       Exactly 2 times
       :            Match the character “:” literally
       \d           Match a single digit 0..9
          {2}       Exactly 2 times
       :            Match the character “:” literally
       \d           Match a single digit 0..9
          {2}       Exactly 2 times
       \.           Match the character “.” literally
       \d           Match a single digit 0..9
          {3}       Exactly 3 times
       \s           Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
          *         Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
       \d           Match a single digit 0..9
          {4}       Exactly 4 times
    )
    .               Match any single character that is not a line break character
       *            Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
    GUID\           Match the characters “GUID ” literally
    (               Match the regular expression below and capture its match into backreference number 2
       [\d.]        Match a single character present in the list below
                    A single digit 0..9
                    The character “.”
          +         Between one and unlimited times, as many times as possible, giving back as needed (greedy)
    )
    

    【讨论】:

    • 是的,伙计,这真是太棒了。它正是我需要的。我对正则表达式(阅读障碍)有很多麻烦。也许它应该被称为阅读障碍。你不必,但如果你想解释它是如何工作的,我很想知道。我已经阅读了教程,但我陷入了他们晦涩难懂的例子中。无论哪种方式都适合您!
    • @dbutts 我已经添加了解释。干杯;)
    猜你喜欢
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多