逐行解析文本文件,如果匹配则提取字符串

时间:2018-06-22 12:52:37

标签: arrays regex powershell parsing text

我们有一个类似于以下内容的文本日志文件,有很多用户

<user>sandip</user>
something
<time>4:38 PM</time>
anything
<elapsed time> 60 mins </elapsed time>

我们要提取所有用户,我们只需通过

Get-Content "C:\LOG\test.txt" | Select-String '(<user>.+</user>)' | ForEach-Object {
  $_.Matches[0].Groups[1].Value
}

我们要逐行解析测试文件,请检查其中是否包含
user/time/elapsed time并[根据需要将其插入动态变量中]构成相同的表

2 个答案:

答案 0 :(得分:1)

考虑到您的日志文件遵循我测试过的相同格式: (即类似这样的内容:)

LogFile

此代码应该可以正常工作:

*

$Lines = get-content .\log.txt
$array = @()
foreach ($line in $lines)
{
if($line -like "<user>*")
{
$obj = New-Object psobject
Add-Member -InputObject $obj -MemberType NoteProperty -Name "UserName" -value $line.Replace("<user>","").Replace("</user>","")
}
if($line -like "<time>*")
{
Add-Member -InputObject $obj -MemberType NoteProperty -Name "Time" -value $line.Replace("<time>","").Replace("</time>","")
}
if($line -like "<elapsed time>*")
{
Add-Member -InputObject $obj -MemberType NoteProperty -Name "ElapsedTime" -value $line.Replace("<elapsed time>","").Replace("</elapsed time>","")
$array += $obj
}
}
$array | Export-Csv .\test.csv

*

答案 1 :(得分:0)

使用两个正则表达式的紧凑型解决方案

  1. 使用非消费类positive lookahead 将源文件分成以<user>
  2. 开头的部分
  3. 在if内,将当前行与alternationbackreference匹配,以将找到的键和值插入到新表的当前行中。在regex101.com
  4. 上实时观看此RegEx

## Q:\Test\2018\06\22\SO_50988379.ps1

$Table = ForEach ($Section in ((Get-Content .\Test.log -raw) -split '(?=<user>)' -ne '')) {
    $Row = New-Object psobject
    ForEach ($Line in ($Section -split "`r?`n")) {
        if($Line -match "<(user|time|elapsed time)>([^<]+)</\1>"){
            Add-Member -InputObject $Row -MemberType NoteProperty `
                -Name "$($Matches[1])" -value $Matches[2].Trim()
        }
    }
    $Row
}
$Table #| Export-Csv .\test.csv

示例输出:

> .\SO_50988379.ps1

user   time    elapsed time
----   ----    ------------
sandip 4:38 PM 60 mins
Joshi  8:15 PM 60 mins