Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, February 10, 2012 3:15 PM
how do I split on pasted data that has different line break types
\n
\n\r
\r\n
\r
is there a way to do that in one line that will get all 4 cases of line
delimiters?
Justin Rich
http://jrich523.wordpress.com
PowerShell V3 Guide (Technet)
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
All replies (13)
Friday, February 10, 2012 3:41 PM ✅Answered | 6 votes
This should work if your split shouldn't produce any nulls:
$str = "abc`rabc`nabc`r`nabc`n`r"($str -split '[\r\n]') |? {$_} abcabcabcabc
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Friday, February 10, 2012 3:23 PM
A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.
[environment]::NewLine
Friday, February 10, 2012 3:28 PM
Hi Justin,
$pastedText -split '\r\n?|\n\r?' # not tested
@Kazun
[Environment]::NewLine depends on environment:
[regex]::Escape([environment]::newline)
Friday, February 10, 2012 3:34 PM | 1 vote
got this to work... not sure it’s the best option
-split '["\n\r"|"\r\n"|\n|\r]'
Justin Rich
http://jrich523.wordpress.com
PowerShell V3 Guide (Technet)
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Friday, February 10, 2012 3:57 PM | 4 votes
\r it's a not new line,this is a Carriage return.
And you don't need to escape NewLine.
PS > "abc`nabc`r`nabc`n`rabc" -split [environment]::NewLineabc
abc
abc
abc
Friday, February 10, 2012 4:03 PM
Compare:
"abc`nabc`r`nabc`n`r" -replace [environment]::NewLine,'_'
"abc`nabc`r`nabc`n`r" -replace '\r\n?|\n\r?','_'
Not the same.
Friday, February 10, 2012 4:05 PM
I have some data that just has the `r, not sure why or now, but that’s the
way it is... so I'll have to go with rob's for safety
Justin Rich
http://jrich523.wordpress.com
PowerShell V3 Guide (Technet)
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Friday, February 10, 2012 4:05 PM
Sure, but remove the double quotes, otherwise you's split on them also.
Friday, February 10, 2012 4:06 PM
Does it matter what you call it? The question is how to split on it.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Friday, February 10, 2012 5:34 PM
mjolinor...
Is there any reason why you choose [\r\n] instead of '\s'? The results are the same but I'm curious if\when it would make a difference...
$str = "abc`rabc`nabc`r`nabc`n`r"
($str -split '\s') |? {$_}
Joe
Friday, February 10, 2012 6:01 PM
That test data in $str was just to see if it would split as expected on carraige returns and newlines.
The production data it's going to be appied to could and probably does have whitespace characters (spaces, tabs, etc.) within the lines that we don't want to split on.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Friday, February 10, 2012 6:49 PM
See
http://msdn.microsoft.com/en-us/library/20bw873z(v=vs.71).aspx
\s is actually more complex than I thought it was.
from that article:
"\s Matches any white-space character. Equivalent to the Unicode character categories
[\f\n\r\t\v\x85\p{Z}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \s
is equivalent to [ \f\n\r\t\v]."
Now I need to go research what the ECMAScript option is all about and if it overlaps with writing
PowerShell code.
Wednesday, January 4, 2017 3:55 PM
Thanks. This worked for me too.
- Slow is smooth and smooth is fast.