Removing more than one white space in powershell
Asked Answered
B

3

27

I was trying to find a way in powershell to remove more than one white space.

But what i found is how to do it in php. "Removing more than one white-space"

There will be similar regular expression may available .

How to acheive the same in powershell?

My string is like this

Xcopy Source  Desination

Some lines may contain more than one white space between Source and destination.

Branen answered 31/12, 2011 at 6:46 Comment(0)
U
78

If you're looking to collapse multiple consecutive whitespace characters into a single space then you can do this using the -replace operator. Given...

PS> $beforeReplace = '   [   Hello,   World!   ]   '
PS> $beforeReplace
   [   Hello,   World!   ]   
PS> $beforeReplace.Length
29

...you would call the -replace operator like this...

PS> $afterReplace = $beforeReplace -replace '\s+', ' '
PS> $afterReplace
 [ Hello, World! ] 
PS> $afterReplace.Length
19

The first parameter to -replace is a regular expression pattern to match, and the second parameter is the text that will replace any matches. \s will match a whitespace character, and + indicates to match one or more occurrences, so, in other words, one or more adjacent whitespace characters will be replaced with a single space.

Replacement without whitespace normalization

If you don't need to normalize all whitespace characters to spaces and, thus, it's ok for standalone whitespace characters to be left untouched, then for long strings you might see better performance with this variation...

PS> $afterReplace = $beforeReplace -replace '\s{2,}', ' '
PS> $afterReplace
 [ Hello, World! ] 
PS> $afterReplace.Length
19

The \s{2,} uses a quantifier meaning "match the preceding element at least two times"; therefore, standalone whitespace characters will not be replaced. When the input string contains a mix of whitespace characters...

PS> $beforeReplace = "1Space: ;2Space:  ;1Tab:`t;2Tab:`t`t;1Newline:`n;2Newline:`n`n;"
PS> $beforeReplace
1Space: ;2Space:  ;1Tab:    ;2Tab:      ;1Newline:
;2Newline:

;
PS> $beforeReplace.Length
57

...note how the results for the two approaches differ...

PS> $afterReplaceNormalized = $beforeReplace -replace '\s+', ' '
PS> $afterReplaceNormalized
1Space: ;2Space: ;1Tab: ;2Tab: ;1Newline: ;2Newline: ;
PS> $afterReplaceNormalized.Length
54
PS> $afterReplaceUnnormalized = $beforeReplace -replace '\s{2,}', ' '
PS> $afterReplaceUnnormalized
1Space: ;2Space: ;1Tab: ;2Tab: ;1Newline:
;2Newline: ;
PS> $afterReplaceUnnormalized.Length
54

While both yield strings of the same length, the unnormalized replacement leaves the single space, single tab, and single newline whitespace runs unmodified. This would work just the same whether adjacent whitespace characters are identical or not.

Additional documentation

Undulate answered 31/12, 2011 at 6:56 Comment(5)
Thanks Mr.Bacon. It does the job for me.Branen
Using Trim(), TrimStart(), and/or TrimEnd() is an unequivocally better option.Transferase
Firstly, my answer is a working, alternative solution, so I don't see why it deserves a downvote. Secondly, the Trim*() methods operate specifically on the beginning and/or end of a string, whereas the author gave an example of a string where the spaces to be removed are in the middle of a string, which Trim*() would ignore.Undulate
Up-voted, as I agree with BACON's reason, regarding the limitation of "trim" command set. Second, this is a solution I will use.Lanoralanose
@RobertAllanHenniganLeahy Using Trim(), TrimStart(), and/or TrimEnd() is an unequivocally wrong answer, but also do you think that if it weren't, there would be a friendlier way stating that?Dystrophy
S
1
'[     Hello,     World!     ]' -replace '^\[\s+','[' -replace '\s+]$',']' -replace '\s+',' '

Bacon has the explanation powershell is funny. I had to escape '[' with '\' even though I know powershells escape character is `.

Santana answered 2/9, 2020 at 3:14 Comment(1)
While yes, the PS escape character is ` that would only work inside " double-quoted strings, not inside the ' single-quoted string in your example, which is actually regex syntax, where `\` is the escape character.Sweeping
M
0

Another way. -split on the left side splits on variable white space:

-split 'Xcopy Source  Destination' -join ' '

Xcopy Source Destination
Marasmus answered 2/9, 2020 at 4:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.