• Login
Community
  • Login

Find line including string, copy this line and replace numbers in xml fie

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
9 Posts 4 Posters 650 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O
    ottiditto
    last edited by Jun 16, 2025, 6:01 PM

    Hello,
    is there any chance in notepad++ to manipulate an xml file in the following manner?

    the xml file contains multiple lines an different positions conraining the string “>NAME”
    The whole line containing this string should be copied directly behind the found line.

    The same line contains a string "layer=“25"”
    After the copying process there are existing always two identical lines with the same content.
    In every second line containing "layer=“25"” the “25” should be replaced by “21”.

    I only managed up to now to bookmark the linemincluding “>NAME” with the Find/Mark Function .

    Thanks for your help!
    best regards

    P 1 Reply Last reply Jun 17, 2025, 4:10 PM Reply Quote 0
    • P
      pbarney @ottiditto
      last edited by pbarney Jun 17, 2025, 4:30 PM Jun 17, 2025, 4:10 PM

      Does >NAME come before layer="25" in a line? (for example, <component name=">NAME" layer="25">) or is it reversed? (for example, <shape type="circle" layer="25">NAME CircleOne</shape>)

      I’ll assume the second case as that’s more typical with XML files.

      Find: (?-s)^((.*? layer=")(25)(".*?>NAME.*\R))
      Replace with: \1\221\4

      Example Input:

      <?xml version="1.0" encoding="UTF-8"?>
      ><document>
      >    <shapes>
      >        <shape type="circle" layer="25">NAME CircleOne</shape>
      >        <shape type="square" layer="25">NAME SquareOne</shape>
      >        <shape type="triangle" layer="25">NAME TriangleOne</shape>
      >        <shape type="hexagon" layer="25">NAME HexagonOne</shape>
      >    </shapes>
      >    <layers>
      >        <layer id="1" layer="25">NAME LayerOne</layer>
      >        <layer id="2" layer="25">NAME LayerTwo</layer>
      >        <layer id="3" layer="25">NAME LayerThree</layer>
      >        <layer id="4" layer="25">NAME LayerFour</layer>
      >    </layers>
      >    <components>
      >        <component id="A" layer="25">NAME ComponentA</component>
      >        <component id="B" layer="25">NAME ComponentB</component>
      >        <component id="C" layer="25">NAME ComponentC</component>
      >        <component id="D" layer="25">NAME ComponentD</component>
      >    </components>
      ></document>
      

      Output:

      <?xml version="1.0" encoding="UTF-8"?>
      <document>
          <shapes>
              <shape type="circle" layer="25">NAME CircleOne</shape>
              <shape type="circle" layer="21">NAME CircleOne</shape>
              <shape type="square" layer="25">NAME SquareOne</shape>
              <shape type="square" layer="21">NAME SquareOne</shape>
              <shape type="triangle" layer="25">NAME TriangleOne</shape>
              <shape type="triangle" layer="21">NAME TriangleOne</shape>
              <shape type="hexagon" layer="25">NAME HexagonOne</shape>
              <shape type="hexagon" layer="21">NAME HexagonOne</shape>
          </shapes>
          <layers>
              <layer id="1" layer="25">NAME LayerOne</layer>
              <layer id="1" layer="21">NAME LayerOne</layer>
              <layer id="2" layer="25">NAME LayerTwo</layer>
              <layer id="2" layer="21">NAME LayerTwo</layer>
              <layer id="3" layer="25">NAME LayerThree</layer>
              <layer id="3" layer="21">NAME LayerThree</layer>
              <layer id="4" layer="25">NAME LayerFour</layer>
              <layer id="4" layer="21">NAME LayerFour</layer>
          </layers>
          <components>
              <component id="A" layer="25">NAME ComponentA</component>
              <component id="A" layer="21">NAME ComponentA</component>
              <component id="B" layer="25">NAME ComponentB</component>
              <component id="B" layer="21">NAME ComponentB</component>
              <component id="C" layer="25">NAME ComponentC</component>
              <component id="C" layer="21">NAME ComponentC</component>
              <component id="D" layer="25">NAME ComponentD</component>
              <component id="D" layer="21">NAME ComponentD</component>
          </components>
      </document>
      

      If, for some reason, >NAME always precedes layer="25", then just swap the elements in the regex like this:

      (?-s)^((.*?>NAME.*? layer=")(25)(".*\R))

      O 2 Replies Last reply Jun 17, 2025, 9:34 PM Reply Quote 1
      • O
        ottiditto @pbarney
        last edited by ottiditto Jun 17, 2025, 9:43 PM Jun 17, 2025, 9:34 PM

        @pbarney Thanks for your reply!
        I will check your suggestion tomorrow and will reply,
        Best regards

        1 Reply Last reply Reply Quote 0
        • O
          ottiditto @pbarney
          last edited by Jun 18, 2025, 8:22 PM

          @pbarney
          Hi, i tested your example and it worked in Notepad++, but unfortunately not with my xml file.

          So here a a few example lines of my xml file:

          <wire x1=“1.4” y1=“-1.09” x2=“1.4” y2=“0.89” width=“0.1” layer=“21” curve=“-294.794621” cap=“flat”/>
          <wire x1=“-0.989” y1=“-0.589” x2=“-0.381” y2=“-0.589” width=“0.0762” layer=“21”/>
          <wire x1=“-0.381” y1=“-0.589” x2=“-0.381” y2=“-1.097” width=“0.0762” layer=“21”/>
          <pad name=“A” x=“-1.3” y=“0” drill=“0.8” diameter=“1.8”/>
          <pad name=“K” x=“1.3” y=“0” drill=“0.8” diameter=“1.8”/>
          <text x=“-2.3” y=“2.1” size=“0.8” layer=“25” ratio=“12”>>NAME</text>
          <text x=“-0.5” y=“2.1” size=“0.8” layer=“27” ratio=“12”>>VALUE</text>
          <text x=“-2” y=“-2.4” size=“0.9” layer=“27” rot=“R90”>>VALUE</text>

          I hope that you can help me further!
          best regards, thanks a lot

          P O 2 Replies Last reply Jun 18, 2025, 8:36 PM Reply Quote 0
          • P
            PeterJones @ottiditto
            last edited by PeterJones Jun 18, 2025, 8:38 PM Jun 18, 2025, 8:36 PM

            @ottiditto ,

            Unfortunately, because you did not use the </> button to wrap your text in a “code block”, the forum interprets parts of your XML as HTML, and displays the quotes as smart-quotes, among other formatting changes – so we cannot tell which of the following you actually intended:

            <wire x1="1.4" y1="-1.09" x2="1.4" y2="0.89" width="0.1" layer="21" curve="-294.794621" cap="flat"/>
            <wire x1="-0.989" y1="-0.589" x2="-0.381" y2="-0.589" width="0.0762" layer="21"/>
            <wire x1="-0.381" y1="-0.589" x2="-0.381" y2="-1.097" width="0.0762" layer="21"/>
            <pad name="A" x="-1.3" y="0" drill="0.8" diameter="1.8"/>
            <pad name="K" x="1.3" y="0" drill="0.8" diameter="1.8"/>
            <text x="-2.3" y="2.1" size="0.8" layer="25" ratio="12">&gt;NAME</text>
            <text x="-0.5" y="2.1" size="0.8" layer="27" ratio="12">&gt;VALUE</text>
            <text x="-2" y="-2.4" size="0.9" layer="27" rot="R90">&gt;VALUE</text>
            

            vs

            <wire x1="1.4" y1="-1.09" x2="1.4" y2="0.89" width="0.1" layer="21" curve="-294.794621" cap="flat"/>
            <wire x1="-0.989" y1="-0.589" x2="-0.381" y2="-0.589" width="0.0762" layer="21"/>
            <wire x1="-0.381" y1="-0.589" x2="-0.381" y2="-1.097" width="0.0762" layer="21"/>
            <pad name="A" x="-1.3" y="0" drill="0.8" diameter="1.8"/>
            <pad name="K" x="1.3" y="0" drill="0.8" diameter="1.8"/>
            <text x="-2.3" y="2.1" size="0.8" layer="25" ratio="12">>NAME</text>
            <text x="-0.5" y="2.1" size="0.8" layer="27" ratio="12">>VALUE</text>
            <text x="-2" y="-2.4" size="0.9" layer="27" rot="R90">>VALUE</text>
            

            or whether your actual text is something different.

            (I could only extract as much as I did because I have moderator powers; even then, I cannot guarantee that the forum didn’t change your text.)

            The regex for those two pieces of text is different, so until you clarify, @pbarney may not be able to help you.

            —

            Useful References

            • Template for Search/Replace Questions
            • Formatting Forum Posts
            1 Reply Last reply Reply Quote 0
            • O
              ottiditto
              last edited by Jun 18, 2025, 8:37 PM

              oops, the community website alters the pasted xml-file,
              her is a screenshot of the original example:

              d8590862-1823-4d72-9bda-35d086898a21-grafik.png

              1 Reply Last reply Reply Quote 1
              • O
                ottiditto @ottiditto
                last edited by ottiditto Jun 18, 2025, 8:47 PM Jun 18, 2025, 8:44 PM

                Sorry for the confusion:

                <wire x1="1.4" y1="-1.09" x2="1.4" y2="0.89" width="0.1" layer="21" curve="-294.794621" cap="flat"/>
                <wire x1="-0.989" y1="-0.589" x2="-0.381" y2="-0.589" width="0.0762" layer="21"/>
                <wire x1="-0.381" y1="-0.589" x2="-0.381" y2="-1.097" width="0.0762" layer="21"/>
                <pad name="A" x="-1.3" y="0" drill="0.8" diameter="1.8"/>
                <pad name="K" x="1.3" y="0" drill="0.8" diameter="1.8"/>
                <text x="-2.3" y="2.1" size="0.8" layer="25" ratio="12">&gt;NAME</text>
                <text x="-0.5" y="2.1" size="0.8" layer="27" ratio="12">&gt;VALUE</text>
                <text x="-2" y="-2.4" size="0.9" layer="25" rot="R90">&gt;VALUE</text>
                
                
                P 1 Reply Last reply Jun 18, 2025, 8:59 PM Reply Quote 2
                • P
                  PeterJones @ottiditto
                  last edited by PeterJones Jun 18, 2025, 9:00 PM Jun 18, 2025, 8:59 PM

                  @ottiditto ,

                  Then I believe changing the FIND WHAT to (?-s)^((.*? layer=")(25)(".*?>\&gt;NAME.*\R)) should do what you want – it now looks for the closing > followed by the &gt; then NAME, and will duplicate a line that matches and change the 25 in the second to a 21, with the following output:

                  <wire x1="1.4" y1="-1.09" x2="1.4" y2="0.89" width="0.1" layer="21" curve="-294.794621" cap="flat"/>
                  <wire x1="-0.989" y1="-0.589" x2="-0.381" y2="-0.589" width="0.0762" layer="21"/>
                  <wire x1="-0.381" y1="-0.589" x2="-0.381" y2="-1.097" width="0.0762" layer="21"/>
                  <pad name="A" x="-1.3" y="0" drill="0.8" diameter="1.8"/>
                  <pad name="K" x="1.3" y="0" drill="0.8" diameter="1.8"/>
                  <text x="-2.3" y="2.1" size="0.8" layer="25" ratio="12">&gt;NAME</text>
                  <text x="-2.3" y="2.1" size="0.8" layer="21" ratio="12">&gt;NAME</text>
                  <text x="-0.5" y="2.1" size="0.8" layer="27" ratio="12">&gt;VALUE</text>
                  <text x="-2" y="-2.4" size="0.9" layer="25" rot="R90">&gt;VALUE</text>
                  
                  1 Reply Last reply Reply Quote 2
                  • G
                    guy038
                    last edited by Jun 19, 2025, 1:45 PM

                    Hello, @ottiditto, @pbarney, @peterjones and All,

                    A tiny piece of information : The & character is not a regex character, so the following syntax is sufficient :

                    • FIND (?-s)^((.*? layer=")(25)(".*?>&gt;NAME.*\R))

                    • REPLACE ${1}${2}21${4}

                    Best Regards,

                    guy038

                    1 Reply Last reply Reply Quote 1
                    8 out of 9
                    • First post
                      8/9
                      Last post
                    The Community of users of the Notepad++ text editor.
                    Powered by NodeBB | Contributors