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 ?
powershell powershell-v3.0 powershell-v4.0
add a comment |
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 ?
powershell powershell-v3.0 powershell-v4.0
You expect thejava.lang.NullPointerException: null
line to be part of the message from the previous line?
– Mathias R. Jessen
Nov 20 '18 at 11:23
Yes. Exactly :)
– Adeel ASIF
Nov 20 '18 at 11:34
add a comment |
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 ?
powershell powershell-v3.0 powershell-v4.0
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 ?
powershell powershell-v3.0 powershell-v4.0
powershell powershell-v3.0 powershell-v4.0
asked Nov 20 '18 at 11:06


Adeel ASIFAdeel ASIF
1,18962037
1,18962037
You expect thejava.lang.NullPointerException: null
line to be part of the message from the previous line?
– Mathias R. Jessen
Nov 20 '18 at 11:23
Yes. Exactly :)
– Adeel ASIF
Nov 20 '18 at 11:34
add a comment |
You expect thejava.lang.NullPointerException: null
line to be part of the message from the previous line?
– Mathias R. Jessen
Nov 20 '18 at 11:23
Yes. Exactly :)
– Adeel ASIF
Nov 20 '18 at 11:34
You expect the
java.lang.NullPointerException: null
line to be part of the message from the previous line?– Mathias R. Jessen
Nov 20 '18 at 11:23
You expect the
java.lang.NullPointerException: null
line to be part of the message from the previous line?– Mathias R. Jessen
Nov 20 '18 at 11:23
Yes. Exactly :)
– Adeel ASIF
Nov 20 '18 at 11:34
Yes. Exactly :)
– Adeel ASIF
Nov 20 '18 at 11:34
add a comment |
3 Answers
3
active
oldest
votes
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 ofImport-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
add a comment |
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]
}
It worked like a charm when I tested. perfecto :)
– Ranadip Dutta
Nov 20 '18 at 11:49
add a comment |
You can achieve the desired result with some preprocessing of your text data.
Get-Content -Path C:tempserver2.log |
ForEach-Object -Begin { $t = '' } `
-Process { if( $_ -match '^d{4}(-dd){2} (dd:){2}dd,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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391647%2fhow-to-delete-carriage-return-in-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 ofImport-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
add a comment |
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 ofImport-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
add a comment |
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 ofImport-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
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 ofImport-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
edited Nov 20 '18 at 12:32
answered Nov 20 '18 at 12:00


LotPingsLotPings
18.4k61532
18.4k61532
add a comment |
add a comment |
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]
}
It worked like a charm when I tested. perfecto :)
– Ranadip Dutta
Nov 20 '18 at 11:49
add a comment |
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]
}
It worked like a charm when I tested. perfecto :)
– Ranadip Dutta
Nov 20 '18 at 11:49
add a comment |
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]
}
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]
}
answered Nov 20 '18 at 11:42


Mathias R. JessenMathias R. Jessen
57k458103
57k458103
It worked like a charm when I tested. perfecto :)
– Ranadip Dutta
Nov 20 '18 at 11:49
add a comment |
It worked like a charm when I tested. perfecto :)
– Ranadip Dutta
Nov 20 '18 at 11:49
It worked like a charm when I tested. perfecto :)
– Ranadip Dutta
Nov 20 '18 at 11:49
It worked like a charm when I tested. perfecto :)
– Ranadip Dutta
Nov 20 '18 at 11:49
add a comment |
You can achieve the desired result with some preprocessing of your text data.
Get-Content -Path C:tempserver2.log |
ForEach-Object -Begin { $t = '' } `
-Process { if( $_ -match '^d{4}(-dd){2} (dd:){2}dd,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
add a comment |
You can achieve the desired result with some preprocessing of your text data.
Get-Content -Path C:tempserver2.log |
ForEach-Object -Begin { $t = '' } `
-Process { if( $_ -match '^d{4}(-dd){2} (dd:){2}dd,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
add a comment |
You can achieve the desired result with some preprocessing of your text data.
Get-Content -Path C:tempserver2.log |
ForEach-Object -Begin { $t = '' } `
-Process { if( $_ -match '^d{4}(-dd){2} (dd:){2}dd,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
You can achieve the desired result with some preprocessing of your text data.
Get-Content -Path C:tempserver2.log |
ForEach-Object -Begin { $t = '' } `
-Process { if( $_ -match '^d{4}(-dd){2} (dd:){2}dd,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
edited Nov 20 '18 at 12:17
answered Nov 20 '18 at 11:58


Andrei OdegovAndrei Odegov
1,309915
1,309915
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391647%2fhow-to-delete-carriage-return-in-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You expect the
java.lang.NullPointerException: null
line to be part of the message from the previous line?– Mathias R. Jessen
Nov 20 '18 at 11:23
Yes. Exactly :)
– Adeel ASIF
Nov 20 '18 at 11:34