Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: change text in XML files  (Read 8934 times)

0 Members and 1 Guest are viewing this topic.

ghostdog74



    Specialist

    Thanked: 27
    Re: change text in XML files
    « Reply #15 on: March 24, 2010, 06:36:22 AM »
    that little batch snippet i wrote is all you need to change all xml files under the directory passed using sed. what more is it you require? in fact , i added the for loop only for doing recursive searching, otherwise if your xml files are all in one directory, then  this is all you need.
    Code: [Select]
    sed -i.bak "s/old/new/g" *.xml

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: change text in XML files
    « Reply #16 on: March 24, 2010, 06:37:01 AM »
    Actually, from the looks of that code, it will take all the .xml files from a directory and perform the word switch for each. (But I don't have sed, so ghostdog will need to confirm my thoughts)
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    ghostdog74



      Specialist

      Thanked: 27
      Re: change text in XML files
      « Reply #17 on: March 24, 2010, 06:38:52 AM »
      Actually, from the looks of that code, it will take all the .xml files from a directory and perform the word switch for each. (But I don't have sed, so ghostdog will need to confirm my thoughts)
      see reply 15

      Helpmeh



        Guru

      • Roar.
      • Thanked: 123
        • Yes
        • Yes
      • Computer: Specs
      • Experience: Familiar
      • OS: Windows 8
      Re: change text in XML files
      « Reply #18 on: March 24, 2010, 06:39:36 AM »
      see reply 15
      Sorry, you must have gotten in right before me, lol...
      Where's MagicSpeed?
      Quote from: 'matt'
      He's playing a game called IRL. Great graphics, *censored* gameplay.

      greg



        Intermediate

        Thanked: 7
        Re: change text in XML files
        « Reply #19 on: March 24, 2010, 08:51:04 AM »
        Code: [Select]
        sed -i.bak "s/old/new/g" *.xml


        The above sed does not work. The original xml file remains the same

        Example:

        C:\batch>type ghost24.bat
        Code: [Select]
        @echo off
        Rem The sed change is displayed to the screen only
        rem The original xml remains unchanged
        echo type envmig.xml
        type  envmig.xml

        sed  "s/newfile.xml/new/g" envmig.xml

        echo type envmig.xml
        type  envmig.xml

        Output:

        C:\batch>ghost24.bat
        type envmig.xml
        newfile.xml
        new
        type envmig.xml
        newfile.xml
        C:\batch>
        Have a Nice Day

        ghostdog74



          Specialist

          Thanked: 27
          Re: change text in XML files
          « Reply #20 on: March 24, 2010, 09:56:40 AM »
          it remains unchanged because you did not redirect to a new file or use -i.
          sed by default doesn't change files.
          please look at my code again and compare it to yours. Its totally different. Why do you produce different code and say it does not work. ?

          greg



            Intermediate

            Thanked: 7
            Re: change text in XML files
            « Reply #21 on: March 24, 2010, 11:35:23 AM »
            it remains unchanged because you did not redirect to a new file or use -i.
            sed by default doesn't change files.
            please look at my code again and compare it to yours. Its totally different. Why do you produce different code and say it does not work. ?

            Ghost,

            Thanks for your help.

            I must have a different version of sed than you do.

            I cannot get my sed to work with your code.


            C:\batch>sed -i.bak "s/HKCU/new/g" envmig.xml
            Unknown option "-i.bak"
            Usage: sed [-nE] [script] [-e script] [-f scriptfile] [file ...]

            C:\batch>

            ______________________________


            C:\batch>type   envmig.xml  |  more
            <migration urlid="http://www.microsoft.com/migration/1.0/migxmlext/oobeupgrade">

              <component type="System" context="User">
                <displayName>oobeUpgrade</displayName>
                <paths>
                  <path type="File">%WINDIR%\oobeUpgrade</path>
                </paths>
                <role role="Settings">

                  <rules>
                    <detects>
                      <detect>
                        <condition negation="Yes">MigXmlHelper.IsOSLaterThan("NT","6.0.0.0")
            </condition>
                      </detect>
                    </detects>
                    <rules>
                      <detects>
                        <detect>
                          <condition negation="Yes">MigXmlHelper.DoesObjectExist("Registry",
            "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders [My
            Music]")</condition>
                        </detect>
                        <detect>
                          <condition negation="Yes">MigXmlHelper.IsSameStringContent...
            .
            .
            .
            Have a Nice Day

            ghostdog74



              Specialist

              Thanked: 27
              Re: change text in XML files
              « Reply #22 on: March 24, 2010, 11:44:09 AM »
              why are you using crippled sed?? see reply 13 on where to download GNU sed.  All my *nix tools are from GNU, not mks. and its free..

              greg



                Intermediate

                Thanked: 7
                Re: change text in XML files
                « Reply #23 on: March 24, 2010, 11:47:13 AM »
                why are you using crippled sed?? see reply 13 on where to download GNU sed.  All my *nix tools are from GNU, not mks. and its free..

                Please show me the output for your sed.

                sed -i.bak "s/old/new/g" *.xml
                Have a Nice Day

                ghostdog74



                  Specialist

                  Thanked: 27
                  Re: change text in XML files
                  « Reply #24 on: March 24, 2010, 11:54:30 AM »
                  it doesn't take 5 minutes to download and try it out yourself.

                  greg



                    Intermediate

                    Thanked: 7
                    Re: change text in XML files
                    « Reply #25 on: March 24, 2010, 02:47:23 PM »
                    It doesn't take 5 minutes to download and try it out yourself.

                    The gnu sed did work:
                    sed -i.bak "s/HKCU/new/g" envmig.xml

                    The redirection is implicit with -i.bak.  envmig.xml.bak is created in the same directory.  Then envmig.xml.bak  is copied over envmig.xml.  The changes to envmig.xml appear to occur in place.

                    One line of sed code to replace the long batch file above.

                    Command line arguments could be used for "old" and "new".  Then the sed
                    code sed -i.bak "s/old/new/g" would not need to be changed each time.

                    Output:

                    C:\batch>c:\bin\sed -i.bak "s/HKCU/new/g" envmig.xml


                    C:\batch>type  envmig.xml  | more
                    <migration urlid="http://www.microsoft.com/migration/1.0/migxmlext/oobeupgrade">

                      <component type="System" context="User">
                        <displayName>oobeUpgrade</displayName>
                        <paths>
                          <path type="File">%WINDIR%\oobeUpgrade</path>
                        </paths>
                        <role role="Settings">

                          <rules>
                            <detects>
                              <detect>
                                <condition negation="Yes">MigXmlHelper.IsOSLaterThan("NT","6.0.0.0")
                    </condition>
                              </detect>
                            </detects>
                            <rules>
                              <detects>
                                <detect>
                                  <condition negation="Yes">MigXmlHelper.DoesObjectExist("Registry",
                    "new\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders [My M
                    usic]")</condition>
                                </detect>
                                <detect>

                    Input:

                    C:\batch>type  envmig.xml |  more
                    <migration urlid="http://www.microsoft.com/migration/1.0/migxmlext/oobeupgrade">

                      <component type="System" context="User">
                        <displayName>oobeUpgrade</displayName>
                        <paths>
                          <path type="File">%WINDIR%\oobeUpgrade</path>
                        </paths>
                        <role role="Settings">

                          <rules>
                            <detects>
                              <detect>
                                <condition negation="Yes">MigXmlHelper.IsOSLaterThan("NT","6.0.0.0")
                    </condition>
                              </detect>
                            </detects>
                            <rules>
                              <detects>
                                <detect>
                                  <condition negation="Yes">MigXmlHelper.DoesObjectExist("Registry",
                    "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders [My
                    Music]")</condition>
                                </detect>
                                <detect>
                                  <condition negation="Yes">MigXmlHelper.IsSameStringContent
                    Have a Nice Day

                    Helpmeh



                      Guru

                    • Roar.
                    • Thanked: 123
                      • Yes
                      • Yes
                    • Computer: Specs
                    • Experience: Familiar
                    • OS: Windows 8
                    Re: change text in XML files
                    « Reply #26 on: March 24, 2010, 05:44:06 PM »
                    One line of sed code to replace the long batch file above.
                    EXACTLY! Ghost said that a the top of the page in reply #15!
                    Where's MagicSpeed?
                    Quote from: 'matt'
                    He's playing a game called IRL. Great graphics, *censored* gameplay.

                    greg



                      Intermediate

                      Thanked: 7
                      Re: change text in XML files
                      « Reply #27 on: March 26, 2010, 07:04:32 AM »
                      Actually, from the looks of that code, it will take all the .xml files from a directory and perform the word switch for each. (But I don't have sed, so ghostdog will need to confirm my thoughts)

                      If Helpmeh does not have have sed then Helpmeh was unable to test the code. Helpmeh doesn't test the code or show output.

                      Helpmeh wrote: (post #26 )
                      "EXACTLY! Ghost said that a the top of the page in reply #15!"
                      Have a Nice Day