INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
(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

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

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

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

(OP)
Completely brilliant!

Thank you so much, I understand, one can simply keep adding 'replace-pairs'.

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

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.

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

(OP)
Yes this works perfectly!

Thanks

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close