regex 选择字符串正则表达式

uelo1irk  于 2023-05-30  发布在  其他
关注(0)|答案(2)|浏览(156)

我正在使用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之前
简而言之,我尝试使用lookbehind(或lookforward)或match来执行正则表达式,这将向$logfile返回类似下面的内容

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

任何帮助感激不尽。我对正则表达式很反感。

lrl1mhuk

lrl1mhuk1#

一种方法就是这样做

$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

说明:

  • 我将switch -SimpleMatch添加到Select-String cmdlet中,因为您似乎希望完全匹配$text,并且由于它在那里不使用regex,因此这将是最佳选择。
  • Select-Object -ExpandProperty Line可以返回一个匹配行的数组,因此我将其通过管道传递给ForEach-Object,以循环通过该数组
  • if (..)使用正则表达式-match,如果该条件为$true,则执行大括号内的任何操作。

此外,这个测试(如果$true)会自动设置一个$matches对象数组,我们使用这些匹配输出一个逗号分隔的行,然后将其收集到变量$result中。

  • 最后,我们简单地将$result输出到一个文件中

Regex详细信息:

(               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)
)
alen0pnh

alen0pnh2#

使用Pattern属性过滤文件内容

$string=Select-String -Path .\batchvariables.bat -Pattern 'TEST_VERSION=(.*)'
$string.Matches.groups[1].value

相关问题