如何在 Powershell 中删除回车 return?
How to delete carriage return in Powershell?
这是我的日志文件:
2018-11-16 01:55:57,102 ERROR [LLLL-1 task-22] c.f.s.p.search.SearchResourceHelper - Error while processing request http://test.com/search-webservice/search?n=20&sf=9-900_60000%2c23-55616&s=1&r=0&t=3&nm=425862&sl=1.0&fs=0&nkr=1&m=1&ct=FParamResolverServiceImplParamResolverServiceImplParamResolverServiceImplParamResolverServiceImplParamResolverServiceImpl
2018-11-16 01:55:57,118 ERROR [PPPP-1 task-22] c.f.s.p.search.SearchResourceHelper - null
java.lang.NullPointerException: null
2018-11-16 04:10:34,346 ERROR [YYYY-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - filter key not valid: 6.
2018-11-16 04:10:34,346 ERROR [AAAA-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - Unknown filter key 6
2018-11-16 04:10:34,346 ERROR [CCCC-1 task-13] c.f.s.p.search.SearchResourceHelper - Validation error while processing request http://testParamResolverServiceImplParamResolverServiceImplParamResolverServiceImplParamResolverServiceImplParamResolverServiceImpl
2018-11-16 04:10:34,362 ERROR [XNIO-1 task-13] c.f.s.p.search.SearchResourceHelper - Unknown filter key 6
javax.validation.ValidationException: Unknown filter key 6
这是我的代码:
$date = Get-Date -UFormat '%Y'
Import-Csv -Path C:\server2.log -Delimiter "," -Header Date, Message | Select -First 8
但是,有一辆马车 return 所以我明白了:
Date Message
---- -------
2018-11-16 01:55:57 102 ERROR [XNIO-1 task-22] c.f.s.p.search.SearchResourceHelper - Error while processing...
2018-11-16 01:55:57 118 ERROR [XNIO-1 task-22] c.f.s.p.search.SearchResourceHelper - null
java.lang.NullPointerException: null
2018-11-16 04:10:34 346 ERROR [XNIO-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - filter key not valid: 6.
2018-11-16 04:10:34 346 ERROR [XNIO-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - Unknown filter key 6
2018-11-16 04:10:34 346 ERROR [XNIO-1 task-13] c.f.s.p.search.SearchResourceHelper - Validation error while...
2018-11-16 04:10:34 362 ERROR [XNIO-1 task-13] c.f.s.p.search.SearchResourceHelper - Unknown filter key 6
javax.validation.ValidationException: Unknown filter key 6
但是如何处理回车return?
假设您在有效行之后只有一行额外的行,您可以逐个查看结果对象列表,如果 Date
值看起来不像日期,将其添加到上一条消息并跳过它:
$Records = Import-Csv -Path C:\server2.log -Delimiter "," -Header Date, Message
$FixedRecords = for($i = 0; $i -lt $Records.Count - 1; $i++){
$NextRecord = $Records[$i + 1]
if($NextRecord.Date -notmatch '^\d{4}-\d{2}-\d{2}'){
# Append next record's message to the current one
$Records[$i].Message += $NextRecord.Message
# Skip ahead to the next record
$i++
}
$Records[$i]
}
您可以通过对文本数据进行一些预处理来获得所需的结果。
Get-Content -Path C:\temp\server2.log |
ForEach-Object -Begin { $t = '' } `
-Process { if( $_ -match '^\d{4}(-\d\d){2} (\d\d:){2}\d\d,\d+' ) { $t; $t = $_ }
else { $t = "${t} ${_}" } } `
-End { $t } |
ConvertFrom-Csv -Header Date, Message | Select -First 8 | Format-Table -AutoSize
Date Message
---- -------
2018-11-16 01:55:57 102 ERROR [LLLL-1 task-22] c.f.s.p.search.SearchResourceHelper - Error while processing request http://test.com/search-webservice/search?n=20&sf=9-900_60000%2c23-55616&s=1&r=0&t=3&nm=425862&sl=1.0&fs=0&nkr=1&m=1&ct=FParamResolverServi...
2018-11-16 01:55:57 118 ERROR [PPPP-1 task-22] c.f.s.p.search.SearchResourceHelper - null java.lang.NullPointerException: null
2018-11-16 04:10:34 346 ERROR [YYYY-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - filter key not valid: 6.
2018-11-16 04:10:34 346 ERROR [AAAA-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - Unknown filter key 6
2018-11-16 04:10:34 346 ERROR [CCCC-1 task-13] c.f.s.p.search.SearchResourceHelper - Validation error while processing request http://testParamResolverServiceImplParamResolverServiceImplParamResolverServiceImplParamResolverServiceImplParamResolverService...
2018-11-16 04:10:34 362 ERROR [XNIO-1 task-13] c.f.s.p.search.SearchResourceHelper - Unknown filter key 6 javax.validation.ValidationException: Unknown filter key 6
另一种方法是:
- 首先用
Get-Content
读取文件作为文本(-Raw
参数需要PSv3+)
- 删除所有 CRs not 后跟时间戳(使用带有 negative lookahead 的正则表达式)
- 使用
ConvertFrom-Csv
代替Import-Csv
> (Get-Content .\server2.log -Raw) -Replace "`r?`n(?!2018)" |
ConvertFrom-Csv -Delimiter "," -Header Date, Message | Select -First 8
Date Message
---- -------
2018-11-16 01:55:57 102 ERROR [LLLL-1 task-22] c.f.s.p.search.SearchResourceHelper - Error while processing request http://test.com/search-webservice/search?n=20&sf=9-900_...
2018-11-16 01:55:57 118 ERROR [PPPP-1 task-22] c.f.s.p.search.SearchResourceHelper - null java.lang.NullPointerException: null
2018-11-16 04:10:34 346 ERROR [YYYY-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - filter key not valid: 6.
2018-11-16 04:10:34 346 ERROR [AAAA-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - Unknown filter key 6
2018-11-16 04:10:34 346 ERROR [CCCC-1 task-13] c.f.s.p.search.SearchResourceHelper - Validation error while processing request http://testParamResolverServiceImplParamReso...
2018-11-16 04:10:34 362 ERROR [XNIO-1 task-13] c.f.s.p.search.SearchResourceHelper - Unknown filter key 6javax.validation.ValidationException: Unknown filter key 6
这是我的日志文件:
2018-11-16 01:55:57,102 ERROR [LLLL-1 task-22] c.f.s.p.search.SearchResourceHelper - Error while processing request http://test.com/search-webservice/search?n=20&sf=9-900_60000%2c23-55616&s=1&r=0&t=3&nm=425862&sl=1.0&fs=0&nkr=1&m=1&ct=FParamResolverServiceImplParamResolverServiceImplParamResolverServiceImplParamResolverServiceImplParamResolverServiceImpl
2018-11-16 01:55:57,118 ERROR [PPPP-1 task-22] c.f.s.p.search.SearchResourceHelper - null
java.lang.NullPointerException: null
2018-11-16 04:10:34,346 ERROR [YYYY-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - filter key not valid: 6.
2018-11-16 04:10:34,346 ERROR [AAAA-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - Unknown filter key 6
2018-11-16 04:10:34,346 ERROR [CCCC-1 task-13] c.f.s.p.search.SearchResourceHelper - Validation error while processing request http://testParamResolverServiceImplParamResolverServiceImplParamResolverServiceImplParamResolverServiceImplParamResolverServiceImpl
2018-11-16 04:10:34,362 ERROR [XNIO-1 task-13] c.f.s.p.search.SearchResourceHelper - Unknown filter key 6
javax.validation.ValidationException: Unknown filter key 6
这是我的代码:
$date = Get-Date -UFormat '%Y'
Import-Csv -Path C:\server2.log -Delimiter "," -Header Date, Message | Select -First 8
但是,有一辆马车 return 所以我明白了:
Date Message
---- -------
2018-11-16 01:55:57 102 ERROR [XNIO-1 task-22] c.f.s.p.search.SearchResourceHelper - Error while processing...
2018-11-16 01:55:57 118 ERROR [XNIO-1 task-22] c.f.s.p.search.SearchResourceHelper - null
java.lang.NullPointerException: null
2018-11-16 04:10:34 346 ERROR [XNIO-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - filter key not valid: 6.
2018-11-16 04:10:34 346 ERROR [XNIO-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - Unknown filter key 6
2018-11-16 04:10:34 346 ERROR [XNIO-1 task-13] c.f.s.p.search.SearchResourceHelper - Validation error while...
2018-11-16 04:10:34 362 ERROR [XNIO-1 task-13] c.f.s.p.search.SearchResourceHelper - Unknown filter key 6
javax.validation.ValidationException: Unknown filter key 6
但是如何处理回车return?
假设您在有效行之后只有一行额外的行,您可以逐个查看结果对象列表,如果 Date
值看起来不像日期,将其添加到上一条消息并跳过它:
$Records = Import-Csv -Path C:\server2.log -Delimiter "," -Header Date, Message
$FixedRecords = for($i = 0; $i -lt $Records.Count - 1; $i++){
$NextRecord = $Records[$i + 1]
if($NextRecord.Date -notmatch '^\d{4}-\d{2}-\d{2}'){
# Append next record's message to the current one
$Records[$i].Message += $NextRecord.Message
# Skip ahead to the next record
$i++
}
$Records[$i]
}
您可以通过对文本数据进行一些预处理来获得所需的结果。
Get-Content -Path C:\temp\server2.log |
ForEach-Object -Begin { $t = '' } `
-Process { if( $_ -match '^\d{4}(-\d\d){2} (\d\d:){2}\d\d,\d+' ) { $t; $t = $_ }
else { $t = "${t} ${_}" } } `
-End { $t } |
ConvertFrom-Csv -Header Date, Message | Select -First 8 | Format-Table -AutoSize
Date Message
---- -------
2018-11-16 01:55:57 102 ERROR [LLLL-1 task-22] c.f.s.p.search.SearchResourceHelper - Error while processing request http://test.com/search-webservice/search?n=20&sf=9-900_60000%2c23-55616&s=1&r=0&t=3&nm=425862&sl=1.0&fs=0&nkr=1&m=1&ct=FParamResolverServi...
2018-11-16 01:55:57 118 ERROR [PPPP-1 task-22] c.f.s.p.search.SearchResourceHelper - null java.lang.NullPointerException: null
2018-11-16 04:10:34 346 ERROR [YYYY-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - filter key not valid: 6.
2018-11-16 04:10:34 346 ERROR [AAAA-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - Unknown filter key 6
2018-11-16 04:10:34 346 ERROR [CCCC-1 task-13] c.f.s.p.search.SearchResourceHelper - Validation error while processing request http://testParamResolverServiceImplParamResolverServiceImplParamResolverServiceImplParamResolverServiceImplParamResolverService...
2018-11-16 04:10:34 362 ERROR [XNIO-1 task-13] c.f.s.p.search.SearchResourceHelper - Unknown filter key 6 javax.validation.ValidationException: Unknown filter key 6
另一种方法是:
- 首先用
Get-Content
读取文件作为文本(-Raw
参数需要PSv3+) - 删除所有 CRs not 后跟时间戳(使用带有 negative lookahead 的正则表达式)
- 使用
ConvertFrom-Csv
代替Import-Csv
> (Get-Content .\server2.log -Raw) -Replace "`r?`n(?!2018)" |
ConvertFrom-Csv -Delimiter "," -Header Date, Message | Select -First 8
Date Message
---- -------
2018-11-16 01:55:57 102 ERROR [LLLL-1 task-22] c.f.s.p.search.SearchResourceHelper - Error while processing request http://test.com/search-webservice/search?n=20&sf=9-900_...
2018-11-16 01:55:57 118 ERROR [PPPP-1 task-22] c.f.s.p.search.SearchResourceHelper - null java.lang.NullPointerException: null
2018-11-16 04:10:34 346 ERROR [YYYY-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - filter key not valid: 6.
2018-11-16 04:10:34 346 ERROR [AAAA-1 task-13] c.f.s.p.s.ParamResolverServiceImpl - Unknown filter key 6
2018-11-16 04:10:34 346 ERROR [CCCC-1 task-13] c.f.s.p.search.SearchResourceHelper - Validation error while processing request http://testParamResolverServiceImplParamReso...
2018-11-16 04:10:34 362 ERROR [XNIO-1 task-13] c.f.s.p.search.SearchResourceHelper - Unknown filter key 6javax.validation.ValidationException: Unknown filter key 6