regex 如何在Powershell中提取两个括号之间的数字

c90pui9n  于 2023-03-31  发布在  Shell
关注(0)|答案(1)|浏览(134)

我有一个日志文件,我试图从中提取一个数字,这是2括号之间,然后将该数据导出到CSV。下面是我目前拥有的代码。它成功地拉取日期,时间和信息,沿着拉加载长度,除了我不希望它有单词长度或括号周围的数字。我只是想在一个单独的列的数字。我只关心“加载长度”数字,不需要任何跳动数字。
日志文件:

02/13/2023 05:20:15.371 [S]0 Degree Long Feedback Spring Measurement. Loading Length[0.7357], Positive RunOut[0.0531], Negative Runout[-0.0377]
02/13/2023 05:20:15.434 [S]Status Message (M09): Feedback Spring Runout Meets Tolerance
02/13/2023 05:20:16.729 [S]Status Message (M09): 
02/13/2023 05:20:19.973 [S]Status Message (M09): 
02/13/2023 05:20:21.955 [S]Waiting for (M5187==1) in Position Bit
02/13/2023 05:20:22.501 [S]Waiting for (M5187==1) in Position Bit
02/13/2023 05:20:22.719 [E]Vision Correction 5 Offset. XOffset[-0.0121], YOffset[-0.0167]
02/13/2023 05:20:22.719 [S]Vision Correction 5 Success. XOffset[-0.0121], YOffset[-0.0167]
02/13/2023 05:20:22.766 [S]Status Message (M09): Vision Correction Success
02/13/2023 05:20:24.404 [S]Waiting for (M5187==1) in Position Bit
02/13/2023 05:20:26.026 [S]Status Message (M09): 
02/13/2023 05:20:29.193 [S]Waiting for (M5187==1) in Position Bit
02/13/2023 05:20:29.739 [S]Waiting for (M5187==1) in Position Bit
02/13/2023 05:20:29.911 [E]Vision Correction 5 Offset. XOffset[-0.0024], YOffset[0.0013]
02/13/2023 05:20:29.911 [S]Vision Correction 5 Success. XOffset[-0.0024], YOffset[0.0013], BallRadius[0.0454]
02/13/2023 05:20:29.973 [S]Status Message (M09): Vision Correction Success
02/13/2023 05:20:31.611 [S]Waiting for (M5187==1) in Position Bit
02/13/2023 05:20:33.015 [S]Reading Long Feedback Spring Length[0.7370], Upper Range[0.7470], Lower Range[0.7270]
02/13/2023 05:20:33.015 [S]90 Degree Long Feedback Spring Measurement. Loading Length[0.7357], Positive RunOut[0.0431], Negative Runout[-0.0478]

当前编码

Get-content J:\datalogs\DMDiagnosticData.log| ForEach {
    $line = $_.Trim()
    If ($line.Contains('Degree Long Feedback Spring Measurement')) {
        $str = $line.Split(' ')
        $props = [ordered]@{
            Date = $str[0]
            Time = $str[1]
            Message = $_.SubString(0,$line.IndexOf('Loading Length')).Replace($str[0],'').Replace($str[1],'').Trim()
            Length = $str[9]
        }
        New-Object PsObject -Property $props
    }
}

例如,我希望输出为:

Date       Time         Message                                        Length
----       ----         -------                                        ------
02/13/2023 13:10:11.513 [S]0 Degree Long Feedback Spring Measurement.  0.7360
oxiaedzo

oxiaedzo1#

以下内容可能会有所帮助:

Get-content J:\datalogs\DMDiagnosticData.log | ForEach-Object {
    If ($_.Contains('Degree Long Feedback Spring Measurement')) {
        $str = $_.Split(' ')
        [pscustomobject]@{
            Date    = $str[0]
            Time    = $str[1]
            Message = [regex]::Match($_, '(?<=\s)\[\w].+\.(?=\sLoading Length)').Value
            Length  = [regex]::Match($str[9], '(?<=\[)[^]]+').Value
        }
    }
}

我已经改变了如何使用正则表达式获取Message属性,这种方式似乎更容易,尽管你仍然可以使用你目前拥有的。关于这一点的细节,请参阅https://regex101.com/r/8vM8lm/2
至于获取括号之间的数字,请参见此链接https://regex101.com/r/o5wAmh/1了解详细信息。
这里有一种不同的方法,它使用一个正则表达式来捕获所有感兴趣的值。它还使用switch来读取带有-Regex标志的文件。

switch -File J:\datalogs\DMDiagnosticData.log -Regex {
    '((?:\d{2}/){2}\d{4})\s((?:\d{2}:){2}\d{2}\.\d{3})\s(\[\w].+\.)\sLoading Length\[([^]]+)' {
        [pscustomobject]@{
            Date    = $Matches[1]
            Time    = $Matches[2]
            Message = $Matches[3]
            Length  = $Matches[4]
        }
    }
}

regex的详细信息如下:https://regex101.com/r/HYh6W2/1 .

相关问题