Replace space with new powershell line

Asked

Viewed 724 times

2

How is it possible to run a file . bat that runs powershell to replace spaces with a new line?

I currently have this scenario in the file run.bat:

set id=1
powershell "(gc %id%_01.txt) -replace '[a-zA-Z]','' | Out-File -encoding ASCII %id%_01.txt"

And this content in the file 1_01.txt

Nome Qualquer
15000 qualquer 14999

The desired result is

15000

14999

I tried to modify the command based on some examples I saw.

powershell "(gc %id%_01.txt) -replace '[a-zA-Z]','' -replace ' ','`r`n' | Out-File -encoding ASCII %id%_01.txt"

But the way out is wrong:

`r`n
15000`r`n`r`n14999

1 answer

2


As stated in this comment, single quotes take text literally, to do what you want, use double quotes:

set id=1
powershell "(gc %id%_01.txt) -replace '[a-zA-Z]','' -replace ' ',"""`r`n""" | Out-File -encoding ASCII %id%_01.txt"

You can avoid unnecessary line breaks at the beginning of the file in this way:

set id=1
powershell "(gc %id%_01.txt | Select-Object -Skip 1) -replace '[a-zA-Z]','' -replace ' ',"""`r`n""" | Out-File -encoding ASCII %id%_01.txt"

Reference to Select-Object.

  • 1

    thanks, also worked out this way -replace ' ',"rn"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.