3

In a file I have the kind of line:

I     have lot         of spaces in      me.

and I replace every space by one space with this powershell code:

$String = "I     have lot         of spaces in      me."
while ($String.Contains("  "))
{
$String = $String -replace "  "," "}

the result is :

I have lot of spaces in me.

I would like to do that for each line in a txt file. could you give me the best way to do that?


Part two:

How can I replace something only when there are more than one whitespace with e.g. ;?

The response will be:

;4828;toto;toto;Ticket;0112APPT

and not :

;4828;toto toto;Ticket;0112APPT

To be clear, I would like replace only two White-Space by the Character ;

Paxz
  • 2,959
  • 1
  • 20
  • 34
Loudone
  • 309
  • 1
  • 2
  • 10
  • 3
    `Get-Content yourfile.txt | % {$_ -replace '\s+', ' '}` ? – Paxz Aug 02 '18 at 11:16
  • 0 down vote accept is it possible to keep the while condition? because in the following case : 4828 toto toto Ticket 0112APPT if I want to replace more that one space with the character ; the response will be: ;4828;toto;toto;Ticket;0112APPT and not : ;4828;toto toto;Ticket;0112APPT To be clear, I would like replace only two Space by the Character ; Kind regards – Loudone Aug 02 '18 at 12:22
  • 1
    Dont write it as a comment... edit your question and append it there. – Paxz Aug 02 '18 at 12:24
  • Take a look at my answer, i updated it to fit your second question. – Paxz Aug 02 '18 at 12:28

2 Answers2

6

Like I said in the comments, this should do it for you (atleast in my test):

Get-Content yourfile.txt | % {$_ -replace '\s+', ' '}

Explanation:

Get-Content - Gets Content from given File

| % - foreach line of the content given from Get-Content

$_ -replace '\s+', ' ' - '\s+' stands for one or more whitespaces


If you want to change the content of the File with the replaced strings you can also pipe it to Set-Content and save it in another file:

Get-Content yourfile.txt | % {$_ -replace '\s+', ' '} | Set-Content yourOutputFile.txt

If you want to write to the same file in the pipe, take a look at: Why you dont do it!


Given your second question to ignore single whitespaces in the regex, this is how you would go if you want to replace more than one whitspace with ;.

This will not replace spots with a single whitespace:

Get-Content yourfile.txt | % {$_ -replace '\s\s+', ';'}
Paxz
  • 2,959
  • 1
  • 20
  • 34
  • Many Thanks Paxz it works fine to your tricks I'd also add the rewrite of the file Like that Get-Content yourfile.txt | % {$_ -replace '\s+', ' '} | Set-Content yourfile.txt – Loudone Aug 02 '18 at 11:59
  • @LudovicHertzog Added it to the answer. Remember to [accept](https://stackoverflow.com/help/accepted-answer) the answer that you found most helpful. – Paxz Aug 02 '18 at 12:15
  • @Paxz I think that using `Get-Content | ... | Set-Content` to the same file is not very good practice due to possible data loss (cannot find the link right now) – Robert Dyjas Aug 02 '18 at 12:30
  • @robdy for the example I "set content" to the same file, but in my script I edit a new One – Loudone Aug 02 '18 at 12:43
  • Yea, I just wanted to suggest improvement to the answer (other people might look at that in the future and blindly copy it). – Robert Dyjas Aug 02 '18 at 12:47
  • @robdy yeah I changed it now. If you somehow manage to find the link again, I would love to read it. I can imagine why it would be bad practice, but couldn't find specific explanations on a quick google. – Paxz Aug 02 '18 at 12:51
3

You can do it like this:

(Get-Content '.\TextDocument.txt' -Raw) -replace ' +', ' '

Note that using \s instead of an actual space in the RegEx is an option, but it will remove not just spaces, but such things as tabs and, more crucially, end of line characters.

boxdog
  • 7,894
  • 2
  • 18
  • 27