Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing multiple patterns using the replace function in a single command - can it be done? 1

Status
Not open for further replies.

bazil2

Technical User
Feb 15, 2010
148
DE
(Elementary user)

Hello Everyone,

I would like to replace certain 'forbidden' characters in the values of incoming XML files, namely single quote, double quote and ampersand (I presume < > are not possible due to their use in tags).

My command line looks like this and successfully replaces the & character:

(gc J:\path\to\my\file-in.xml) -replace '&', '&amp;' | Out-File J:\path\to\my\file-out.xml

Is it possible to add additional pairs to find in replace in the same line?

Many thanks
 
Can you do something like this?

(gc J:\path\to\my\file-in.xml) | Foreach-Object{$_ -replace '&', '&amp;' -replace 'something','nothing'} |Out-File J:\path\to\my\file-out.xml
 
Completely brilliant!

Thank you so much, I understand, one can simply keep adding 'replace-pairs'.
 
I had to play with it a bit but this ended up working for me:

(gc J:\path\to\my\file-in.xml) | Foreach-Object{$_ -replace '&', '&amp;' -replace '"',"" -replace "'",''} |Out-File J:\path\to\my\file-out.xml



Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Yes this works perfectly!

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top