Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Search and replace text with &

$
0
0

Hi all,

I'm new to PowerShell and I'm trying to do a simple search and replace in a text file. My scenario is I have a text file, generated daily, that doesn't handle accented characters etc very well. I don't have any means of adjusting the encoding in the service that outputs the file, so I want to schedule a PowerShell script that will run later to fix and re-save the text file for me.

For example, say the original text file test.txt has the following:

Ève,Ève,Boucher,4,123456,7890,2028

Chloé,Chloé,Martin,0,123456,7890,2032

I want to replace it with:

Ève,Ève,Boucher,4,123456,7890,2028

Chloé,Chloé,Martin,0,123456,7890,2032

and so on. So È is replaced with È, é is replaced with é, and so on.

If I try the following:

powershell -Command "(gc test.txt) -replace 'È', 'E' | Out-File -encoding ASCII test2.txt"

I get:

??ve,Eve,Boucher,4,123456,7890,2028
Chlo??,Chloe,Martin,0,123456,7890,203

and if I try -encoding utf8 I get

Ève,Ève,Boucher,4,123456,7890,2028
Chloé,Chloe,Martin,0,123456,7890,2032

I've tried a couple of variations on this but no luck so far. The output I get with the encoding set to ASCII would be fine for my needs, except that in the first field Ève is also rewritten as ??ve. (It did rename the Ève in the second field to Eve however.) I'm not sure why the first field is also being modified.

I'm guessing the & character is being treated as some escape character but am messing up the syntax. Any ideas? Thanks in advance!

Sir_Timbit





Viewing all articles
Browse latest Browse all 21975


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>