• Login
Community
  • Login

Replace strings with specific lines content

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
4 Posts 3 Posters 306 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.
  • A
    Andres Garzon
    last edited by Feb 7, 2022, 4:50 AM

    Hello!

    I have the following file

    45415
    7684
    124
    6845
    3210
    16
    785
    8651
    245
    8521
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    	<RuleList>
    		<Rule enabled="true">
    			<Action type="Proxy">100</Action>
    			<Applications>pid=4323</Applications>
    			<Name>Nox0</Name>
    		</Rule>
    		<Rule enabled="true">
    			<Action type="Proxy">101</Action>
    			<Applications>pid=12</Applications>
    			<Name>Nox1</Name>
    		</Rule>
    		<Rule enabled="true">
    			<Action type="Proxy">102</Action>
    			<Applications>pid=87654</Applications>
    			<Name>Nox2</Name>
    		</Rule>
    		<Rule enabled="true">
    			<Action type="Proxy">103</Action>
    			<Applications>pid=345</Applications>
    			<Name>Nox3</Name>
    		</Rule>
    		<Rule enabled="true">
    			<Action type="Proxy">104</Action>
    			<Applications>pid=936</Applications>
    			<Name>Nox4</Name>
    		</Rule>
    		<Rule enabled="true">
    			<Action type="Proxy">105</Action>
    			<Applications>pid=54734</Applications>
    			<Name>Nox5</Name>
    		</Rule>
    		<Rule enabled="true">
    			<Action type="Proxy">106</Action>
    			<Applications>pid=6435</Applications>
    			<Name>Nox6</Name>
    		</Rule>
    		<Rule enabled="true">
    			<Action type="Proxy">107</Action>
    			<Applications>pid=95325</Applications>
    			<Name>Nox7</Name>
    		</Rule>
    		<Rule enabled="true">
    			<Action type="Proxy">108</Action>
    			<Applications>pid=10</Applications>
    			<Name>Nox8</Name>
    		</Rule>
    		<Rule enabled="true">
    			<Action type="Proxy">109</Action>
    			<Applications>pid=000992</Applications>
    			<Name>Nox9</Name>
    		</Rule>
    	</RuleList>
    

    There are multiple pid=xxxx in the file. I need to replace each xxxx with the numbers on the first 10 lines (each line would replace a xxxx). How can I achieve this? Can it be done with Find and replace?

    I have absolutely no idea about regex (first time I heard about that) but investigating I managed to produce (?<=>pid=).+?(?=</A) to match what I need to replace in “Find What”, but I don’t know what to put in “Replace with” to capture the numbers in the beginning of the file.

    Any help would be greatly appreciated.

    1 Reply Last reply Reply Quote 0
    • G
      guy038
      last edited by guy038 Feb 7, 2022, 9:19 PM Feb 7, 2022, 2:41 PM

      Hello, @andres-garzon and All,

      Here is a solution using regexes, of course !

      Just follow this road map. It may appear a bit complicated but it doesn’t take much time to get the correct results !

      So :

      • First, copy your XML text in a new tab

      • Add a line of, at least, five = signs

      • Append your list of numbers, one per line

      You should obtain this layout :

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      	<RuleList>
      		<Rule enabled="true">
      			<Action type="Proxy">100</Action>
      			<Applications>pid=4323</Applications>
      			<Name>Nox0</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">101</Action>
      			<Applications>pid=12</Applications>
      			<Name>Nox1</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">102</Action>
      			<Applications>pid=87654</Applications>
      			<Name>Nox2</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">103</Action>
      			<Applications>pid=345</Applications>
      			<Name>Nox3</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">104</Action>
      			<Applications>pid=936</Applications>
      			<Name>Nox4</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">105</Action>
      			<Applications>pid=54734</Applications>
      			<Name>Nox5</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">106</Action>
      			<Applications>pid=6435</Applications>
      			<Name>Nox6</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">107</Action>
      			<Applications>pid=95325</Applications>
      			<Name>Nox7</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">108</Action>
      			<Applications>pid=10</Applications>
      			<Name>Nox8</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">109</Action>
      			<Applications>pid=000992</Applications>
      			<Name>Nox9</Name>
      		</Rule>
      	</RuleList>
      =====
      45415
      7684
      124
      6845
      3210
      16
      785
      8651
      245
      8521
      

      • Open the Mark dialog ( Ctrl + M )

      • MARK (?<=pid=)\d+|^\d+$|^=====

      • Untick all options

      • Tick the Wrap around option

      • Select the Regular expression search mode

      • Click on the Mark All button

      • Click on the Copy Marked Text button

      • Select your list of numbers, after the line of equal signs

      • Replace by the result of marking ( Ctrl + V )

      Giving the following text :

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      	<RuleList>
      		<Rule enabled="true">
      			<Action type="Proxy">100</Action>
      			<Applications>pid=4323</Applications>
      			<Name>Nox0</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">101</Action>
      			<Applications>pid=12</Applications>
      			<Name>Nox1</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">102</Action>
      			<Applications>pid=87654</Applications>
      			<Name>Nox2</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">103</Action>
      			<Applications>pid=345</Applications>
      			<Name>Nox3</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">104</Action>
      			<Applications>pid=936</Applications>
      			<Name>Nox4</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">105</Action>
      			<Applications>pid=54734</Applications>
      			<Name>Nox5</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">106</Action>
      			<Applications>pid=6435</Applications>
      			<Name>Nox6</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">107</Action>
      			<Applications>pid=95325</Applications>
      			<Name>Nox7</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">108</Action>
      			<Applications>pid=10</Applications>
      			<Name>Nox8</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">109</Action>
      			<Applications>pid=000992</Applications>
      			<Name>Nox9</Name>
      		</Rule>
      	</RuleList>
      =====
      4323
      12
      87654
      345
      936
      54734
      6435
      95325
      10
      000992
      =====
      45415
      7684
      124
      6845
      3210
      16
      785
      8651
      245
      8521
      

      • Now, place the caret right before the first number of your list ( 45415)

      • Do a rectangular selection ( 10 × 5 ), taking all your list of numbers

      • Hit the Ctrl + C shortcut

      • Move backward and place the caret right after the first present number ( 4323 )

      • Add 4 space characters

      • Hit the Ctrl + V shortcut

      Now, our text becomes :

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      	<RuleList>
      		<Rule enabled="true">
      			<Action type="Proxy">100</Action>
      			<Applications>pid=4323</Applications>
      			<Name>Nox0</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">101</Action>
      			<Applications>pid=12</Applications>
      			<Name>Nox1</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">102</Action>
      			<Applications>pid=87654</Applications>
      			<Name>Nox2</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">103</Action>
      			<Applications>pid=345</Applications>
      			<Name>Nox3</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">104</Action>
      			<Applications>pid=936</Applications>
      			<Name>Nox4</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">105</Action>
      			<Applications>pid=54734</Applications>
      			<Name>Nox5</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">106</Action>
      			<Applications>pid=6435</Applications>
      			<Name>Nox6</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">107</Action>
      			<Applications>pid=95325</Applications>
      			<Name>Nox7</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">108</Action>
      			<Applications>pid=10</Applications>
      			<Name>Nox8</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">109</Action>
      			<Applications>pid=000992</Applications>
      			<Name>Nox9</Name>
      		</Rule>
      	</RuleList>
      =====
      4323    45415
      12      7684
      87654   124
      345     6845
      936     3210
      54734   16
      6435    785
      95325   8651
      10      245
      000992  8521
      =====
      45415
      7684
      124
      6845
      3210
      16
      785
      8651
      245
      8521
      

      • Open the Replace diialog ( Ctrl + H )

      • SEARCH (?s)pid=(\d+)(?=<)(?=.+?^\1\x20+(\d+)$)|^=====.+

      • REPLACE ?1pid=\2

      • Untick All options

      • Tick the Wrap around option

      • Select the Regular expression search mode

      • Click once only on the Replace All button ( or several times on the Replace button till the end of the process ! )

      And you’re left with the expected results ;-))

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      	<RuleList>
      		<Rule enabled="true">
      			<Action type="Proxy">100</Action>
      			<Applications>pid=45415</Applications>
      			<Name>Nox0</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">101</Action>
      			<Applications>pid=7684</Applications>
      			<Name>Nox1</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">102</Action>
      			<Applications>pid=124</Applications>
      			<Name>Nox2</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">103</Action>
      			<Applications>pid=6845</Applications>
      			<Name>Nox3</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">104</Action>
      			<Applications>pid=3210</Applications>
      			<Name>Nox4</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">105</Action>
      			<Applications>pid=16</Applications>
      			<Name>Nox5</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">106</Action>
      			<Applications>pid=785</Applications>
      			<Name>Nox6</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">107</Action>
      			<Applications>pid=8651</Applications>
      			<Name>Nox7</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">108</Action>
      			<Applications>pid=245</Applications>
      			<Name>Nox8</Name>
      		</Rule>
      		<Rule enabled="true">
      			<Action type="Proxy">109</Action>
      			<Applications>pid=8521</Applications>
      			<Name>Nox9</Name>
      		</Rule>
      	</RuleList>
      

      Best Regards,

      guy038

      1 Reply Last reply Reply Quote 1
      • A
        Andres Garzon
        last edited by Feb 7, 2022, 7:25 PM

        Wonderful, worked like a charm!

        Now, is there any way to put all these steps into a macro? I tried doing all the steps while recording macro but when I run it, it doesn’t work;

        Thank you

        Alan KilbornA 1 Reply Last reply Feb 7, 2022, 7:54 PM Reply Quote 0
        • Alan KilbornA
          Alan Kilborn @Andres Garzon
          last edited by Feb 7, 2022, 7:54 PM

          @andres-garzon said in Replace strings with specific lines content:

          is there any way to put all these steps into a macro?

          IMO it would be insane to try to record that as a macro, or to even hand-edit a macro to try to do it.

          I wouldn’t have written to just say that though. I wanted to point out a specific thing as I look over the list of what Guy gave you.

          I see:

          • Click on the Copy Marked Text button

          I’ve noticed that this button on the Mark window isn’t macro-recordable. :-(

          But… happily, this is the equivalent command and it IS macro-recordable:

          d8be9717-a3e2-48c5-8a07-39be38dd4e02-image.png

          As to the rest…good luck with the macroizing!

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