Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace Vertical bar within a text file

I am trying to replace a | character within a text file. But I am not sure how to do it because the batch is not reading the |.

powershell -Command "(gc output.txt) -replace '|', ' ' | Out-File -encoding ASCII output.txt"

Which takes this input:
80853||OHNED|Mira

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

And outputs: 8 0 8 5 3 | | O H N E D | M i r a

Where I’d like this output 80853 OHNED Mira

Is there anyway within a batch to replace the | character?

Edit – While googling, I found out that the | character is called a vertical bar.

>Solution :

The -replace operator is regex-based; since your intent is to replace all | characters verbatim, you must escape | as \|, given that | is a regex metacharacter (a character with special meaning in the context of a regex (regular expression)):

powershell -Command "(gc output.txt) -replace '\|', ' ' | Out-File -encoding ASCII output.txt"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading