Adeel ASIF
Adeel ASIF

Reputation: 3524

How to delete carriage return in Powershell?

This is my log file :

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

This is my code :

$date = Get-Date -UFormat '%Y'

Import-Csv -Path C:\server2.log -Delimiter "," -Header Date, Message | Select -First 8

However, there is a carriage return so i'm getting that :

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

However how to deal with the carriage return ?

Upvotes: 0

Views: 155

Answers (3)

user6811411
user6811411

Reputation:

An alternative is to :

  • first read the file as text with Get-Content (-Raw parameter requires PSv3+)
  • remove all CRs not followed by a time stamp (using a RegEx with a negative lookahead)
  • use ConvertFrom-Csv instead of 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

Upvotes: 2

Andrei Odegov
Andrei Odegov

Reputation: 3429

You can achieve the desired result with some preprocessing of your text data.

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

Upvotes: 1

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174445

Assuming you'll ever only have one extra line after a valid one, you could go through the list of resulting objects one by one, and if the Date value doesn't look like a date, add it to the previous message and skip it:

$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]
}

Upvotes: 3

Related Questions