powershell on command line replace double quoted text

Welcome Forums General PowerShell Q&A powershell on command line replace double quoted text

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
2 months, 2 weeks ago.

  • Author
    Posts
  • #163848

    Participant
    Topics: 1
    Replies: 1
    Points: 15
    Rank: Member

    Hi all

    I have a file containg (multiple) xml tags that contain the attribute type="ABC". The ABC needs to be replaced by XYZ.

    I have tried to use the below (escape with backtick, as it seems to be the standard in powershell) and the variation of it by escaping the double quotes with the backslash (I'm basically a pure C programmer).

     powershell -command "(gc %rootdir%\directory\file3.xml) -replace 'TYPE=`"ABC`"','TYPE=`"XYZ`"' | Out-File %rootdir%\directory\file.xml" -encoding ASCII

    I eventually tried escaping the double quote with another double quote and that worked. The question is where I can read up on escaping in powershell or if somebody can explain why only the escaping the double quote with a double quote worked.

    Thanks in advance

  • #163854

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero
  • #163857

    Participant
    Topics: 1
    Replies: 1529
    Points: 2,587
    Helping Hand
    Rank: Community Hero

    You may read the free Powershell cookbook about working with XML files. I'd recommend to treat an XML file not like plain text. 😉

  • #163863

    Participant
    Topics: 1
    Replies: 1
    Points: 15
    Rank: Member

    Thanks people for the replies.

    I'm aware that I will probably be better of using the XML approach but this is an attempt at a quick and dirty hack to fix a tool-chain after requirements for the output files changed.

You must be logged in to reply to this topic.