Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing more than one white space in powershell

Tags:

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.

like image 593
Samselvaprabu Avatar asked Dec 31 '11 06:12

Samselvaprabu


People also ask

How do I remove spaces from a text file in PowerShell?

We can use following script to remove space at the end of each line in a file with the help of powershell script. $InputFile = 'C:\Users\user\Desktop\1. txt' write-host "removing trailing space.. of file $InputFile" $content = Get-Content $InputFile $content | Foreach {$_. TrimEnd()} | Set-Content newfile.

How do I get rid of whitespace?

The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".

Is PowerShell whitespace sensitive?

White-space is (mostly) irrelevant to PowerShell, but its proper use is key to writing easily readable code. Use a single space after commas and semicolons, and around pairs of curly braces.


1 Answers

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

  • Enter help about_Comparison_Operators [ Windows PowerShell 2.0 ][ PowerShell (Core) ]
  • Enter help about_Regular_Expressions [ Windows PowerShell 2.0 ][ PowerShell (Core) ]
  • .NET Regular Expression Language - Quick Reference
like image 117
Lance U. Matthews Avatar answered Sep 30 '22 18:09

Lance U. Matthews