使用Regex从.log文件中提取数据

时间:2019-03-22 11:26:45

标签: regex powershell

我正尝试使用正则表达式正向后提取数据。我创建了一个具有以下内容的.ps1文件:

$input_path = ‘input.log’

$output_file = ‘Output.txt’

$regex = ‘(?<=    "name": ")(.*)(?=",)|(?<=    "fullname": ")(.*)(?=",)|(?<=Start identity token validation\r\n)(.*)(?=ids: Token validation success)|(?<=  "ClientName": ")(.*)(?=",\r\n  "ValidateLifetime": false,)’

select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } >$output_file

输入文件如下:

08:15.27.47-922: T= 11 ids: Start end session request
08:15.27.47-922: T= 11 ids: Start end session request validation
08:15.27.47-922: T= 11 ids: Start identity token validation
08:15.27.47-922: T= 11 ids: Token validation success
{
  "ClientId": "te_triouser",
  "ClientName": "TE Trio User",
  "ValidateLifetime": false,
  "Claims": {
    "iss": "http://sv-trio17.adm.linkoping.se:34000/core/",
    "aud": "te_triouser",
    "exp": "1552054900",
    "nbf": "1552054600",
    "nonce": "f1ae9044-25f9-4e7f-b39f-bd7bdcb9dc8d",
    "iat": "1552054600",
    "at_hash": "Wv_7nNe42gUP945FO4p0Wg",
    "sid": "9870230d92cb741a8674313dd11ae325",
    "sub": "23223",
    "auth_time": "1551960154",
    "idp": "tecs",
    "name": "tele2",
    "canLaunchAdmin": "1",
    "isLockedToCustomerGroup": "0",
    "customerGroupId": "1",
    "fullname": "Tele2 Servicekonto Test",
    "tokenIdentifier": "2Ljta5ZEovccNlab9QXb8MPXOqaBfR6eyKst/Dc4bF4=",
    "tokenSequence": "bMKEXP9urPigRDUguJjvug==",
    "tokenChecksum": "NINN0DDZpx7zTlxHqCb/8fLTrsyB131mWoA+7IFjGhAV303///kKRGQDuAE6irEYiCCesje2a4z47qvhEX22og==",
    "idpsrv_lang": "sv-SE",
    "CD_UserInfo": "23223 U2 C1",
    "amr": "optional"
  }
}

如果我通过http://regexstorm.net/tester运行正则表达式,则会得到正确的匹配项。但是,当我在计算机上运行带有Powershell的脚本时,我在正则表达式问题中没有\ r \ n的地方找不到匹配项。我只从前两个正则表达式问题中获得匹配。

1 个答案:

答案 0 :(得分:2)

  • 我同意@AdminOfThings将Get-Content与-raw参数一起使用。
  • 也不要在脚本中使用印刷引号。
  • 如果前导空格的数量不是真正固定的,则替换为一个空格和+*量词。
  • \r设置为可选=> \r?

minimal complete verifiable example还应包括您的预期输出。

EDIT修改了正则表达式,使其更易读

以下脚本

## Q:\Test\2019\03\22\SO_55298614.ps1

$input_path = 'input.log'
$output_file = 'Output.txt'

$regexes = ('(?<= *"(full)?name": ")(.*)(?=",)',
            '(?<=Start identity token validation\r?\n)(.*)(?=ids: Token validation success)',
            '(?<= *"ClientName": ")(.*)(?=",\r?\n *"ValidateLifetime": false,)')

$regex = [RegEx]($regexes -join'|')


Get-Content $input_path -Raw | Select-String -pattern $regex -AllMatches | 
   ForEach-Object { $_.Matches.Value }

产生以下示例输出:

> Q:\Test\2019\03\22\SO_55298614.ps1
08:15.27.47-922: T= 11
TE Trio User
tele2
Tele2 Servicekonto Test