Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 拆分日志文件_Powershell - Fatal编程技术网

Powershell 拆分日志文件

Powershell 拆分日志文件,powershell,Powershell,我有一个包含以下内容的日志文件: Wed Oct 17 05:39:27 2018 : Resource = 'test04' cstep= 'titi04' time =18.751s Wed Oct 17 05:40:31 2018 : Resource = 'test05' cstep= 'titi05' time =58.407s Wed Oct 17 05:41:31 2018 : Resource = 'test06' cstep= 'titi06' time =3.400s

我有一个包含以下内容的日志文件:

Wed Oct 17 05:39:27 2018 : Resource = 'test04' cstep= 'titi04' time =18.751s Wed Oct 17 05:40:31 2018 : Resource = 'test05' cstep= 'titi05' time =58.407s Wed Oct 17 05:41:31 2018 : Resource = 'test06' cstep= 'titi06' time =3.400s Wed Oct 17 05:42:31 2018 : Resource = 'test07' cstep= 'titi07' time =4.402s 结果是

18.751 58.407 3.400 4.402 我只要

3.400 4.402
下面将所选字符串强制转换为double,然后仅返回小于5的字符串

$results = Foreach ($line in $list) {
    $val = [double]$line.Split('=')[3].Trim().TrimEnd('s')
    if($val -lt 5) {
        $val
    }
}
选择字符串是一个选项:

(Select-String -Path "TargetLog.txt" -Pattern ".*(?<time>\d+\.\d+)s").Matches |
    ForEach-Object {
        if([double]$_.Groups['time'].Value -lt 5.0) {$_.Value}
    }
如果只需要每行的数字,请将If块更改为:


{$\.Groups['time'].Value}

动态更改需求通常是不可能的, 所以你不配。 另外,Superior 5的措辞提醒了我之前从另一个用户帐户提出的问题

然而,这里有一个带有单管道和日期时间转换的脚本

## Q:\Test\2018\11\06\SO_53170145.ps1
Get-Content .\logfile.txt |
  Where-Object {$_ -match '^(.*?) : .*time =([0-9\.]+)s'}|
    Select-Object @{n='DT';e={([datetime]::ParseExact($Matches[1],'ddd MMM dd HH:mm:ss yyyy',[cultureinfo]::InvariantCulture).ToString('yyyy-MM-dd HH:mm:ss'))}},
                  @{n='val';e={[double]$Matches[2]}} |
      Where-Object val -le 5
由于我的德语区域设置,示例输出十进制逗号

DT                    val
--                    ---
2018-10-17 05:41:31   3,4
2018-10-17 05:42:31 4,402

谢谢,现在我想将2018年10月17日星期三05:41:31转换为2018-10-17 05:41:31,谢谢首先你说你只想要大于5的值,然后你说你只想要小于5的值。这是哪一个?我的新代码是:$list=Get Content C:\Users\\Desktop\log.txt$Results=Foreach$line in$list{$val=[double]$line.Split'=''[3]$line.Split''if$val-lt 5{$line.Split'[3]$line}$Results我想要转换星期三2018年10月17日05:41:31到2018年10月17日05:41:31这不是我想要的结果,我想提取2018年10月17日星期三05:41:31的值,并轻松转换为2018-10-17 05:41:31
Wed Oct 17 05:41:31 2018 : Resource = 'test06' cstep= 'titi06' time =3.400s
Wed Oct 17 05:42:31 2018 : Resource = 'test07' cstep= 'titi07' time =4.402s
## Q:\Test\2018\11\06\SO_53170145.ps1
Get-Content .\logfile.txt |
  Where-Object {$_ -match '^(.*?) : .*time =([0-9\.]+)s'}|
    Select-Object @{n='DT';e={([datetime]::ParseExact($Matches[1],'ddd MMM dd HH:mm:ss yyyy',[cultureinfo]::InvariantCulture).ToString('yyyy-MM-dd HH:mm:ss'))}},
                  @{n='val';e={[double]$Matches[2]}} |
      Where-Object val -le 5
DT                    val
--                    ---
2018-10-17 05:41:31   3,4
2018-10-17 05:42:31 4,402