Community
    • Login

    Attempting to isolate all lines containing a specific word and edit one value.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 966 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.
    • Richard Brock Jr.R
      Richard Brock Jr.
      last edited by

      Hello, -Picture-

      I have been trying to work this out for hours now and I simply can’t figure it out.

      Basically, if you look at the picture, I am trying to isolate all lines in the document (to which there are 300k+) that contain a specific word. The first task would be index=“151”. Every line containing that string I need to change, and only change the ‘color’ code.

      As an example -

      <item parent="9463" index="9476">
      	<block lx="2.34999752" ly="6.58951187" lz="16.3749924" ux="2.39999676" uy="6.63951397" uz="16.8749962" index="151" material="3" look="0" up="3" color="ffcd0000"/>
      </item>
      

      I need this to remain exact the same, except for the ‘color’ string, and I need this done for all items containing the index value 151. To which there are 4606 in the document, but I assume that is irrelevant.

      Also, this is just one of dozens of index color changes I need to make. I have tried other methods, but they break the structure of the document and that corrupts it.

      I suspect there is an simple solution, but nothing I’ve tried has worked.

      Thanks.

      1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones
        last edited by PeterJones

        So, if I’ve understood correctly, any row that has index=“151”, you want to set the color to one specific value? Assuming that they are always on the same line (no newline character), and assuming that the color attribute always comes after the index attribute, then it’s easy:

        • Find = (?-s)(index="151".*color=")([^"]+)(")
          • the first paren makes sure newlines aren’t matched by .*
          • the second looks for the right index followed by the color prefix and puts it into group $1 (aka \1)
          • the third matches the old value of the color attribute into group $2
          • the fourth matches the end-quote for the color attribute, and stores it by its lonesome in $3
        • Replace = $1deadbeef$3 (where I used deadbeef as a fake color; pick whatever color is appropriate for you)
          • the replacement will be the index-thru-color-prefix, followed by deadbeef or whatever literal color you choose, followed by the lonely quote
          • everything else in the line remains the same
        • Search mode = regular expression

        If you have dozens to do, then it will be a little tedious with doing a similar transformation for each.

        There are some on this forum who would be able to help you set up a “lookup table”, where you could define index 151 mapping to color deadbeef, and index 314 mapping to 12345678, etc. But I’m not one of those “some”. You might be able to search the forum for “lookup” to see how it’s done. Or if you can wait patiently for his weekend ski trip to end, @guy038 could whip one up for in a few days.

        -----
        FYI:

        This forum is formatted using Markdown, with a help link buried on the little grey ? in the COMPOSE window/pane when writing your post. For more about how to use Markdown in this forum, please see @Scott-Sumner’s post in the “how to markdown code on this forum” topic, and my updates near the end. It is very important that you use these formatting tips – using single backtick marks around small snippets, and using code-quoting for pasting multiple lines from your example data files – because otherwise, the forum will change normal quotes ("") to curly “smart” quotes (“”), will change hyphens to dashes, will sometimes hide asterisks (or if your text is c:\folder\*.txt, it will show up as c:\folder*.txt, missing the backslash). If you want to clearly communicate your text data to us, you need to properly format it.

        If you have further search-and-replace (“matching”, “marking”, “bookmarking”, regular expression, “regex”) needs, study this FAQ and the documentation it points to. Before asking a new regex question, understand that for future requests, many of us will expect you to show what data you have (exactly), what data you want (exactly), what regex you already tried (to show that you’re showing effort), why you thought that regex would work (to prove it wasn’t just something randomly typed), and what data you’re getting with an explanation of why that result is wrong. When you show that effort, you’ll see us bend over backward to get things working for you. If you need help formatting, see the paragraph above.

        Please note that for all regex and related queries, it is best if you are explicit about what needs to match, and what shouldn’t match, and have multiple examples of both in your example dataset. Often, what shouldn’t match helps define the regular expression as much or more than what should match.

        1 Reply Last reply Reply Quote 1
        • guy038G
          guy038
          last edited by

          Hello @richard-brock-jr, @peterjones and All,

          I personally would use this regex S/R :

          SEARCH (?-s)index="151".*"\K[[:xdigit:]]+(?="/>)

          REPLACE The NEW color


          Now, if you have several couples Index number <–> Color string, you could use this generic regex :

          SEARCH (?-s)index="(?:(N1)|(N2)|(N3)....|(Nn))".*"\K[[:xdigit:]]+(?="/>)

          REPLACE (?1New_Color1)(?2New_Color2)(?3New_Color3)....(?nNew_Colorn)


          For instance, assuming the original text :

          
          <item parent="9463" index="9476">
              <block lx="2.34999752" ly="6.58951187" lz="16.3749924" ux="2.39999676" uy="6.63951397" uz="16.8749962" index="151" material="3" look="0" up="3" color="ffcd0000"/>
          </item>
          <item parent="8956" index="2458">
              <block lx="2.34999752" ly="6.58951187" lz="16.3749924" ux="2.39999676" uy="6.63951397" uz="16.8749962" index="200" material="3" look="0" up="3" color="abcdef"/>
          </item>
          <item parent="1268" index="6327">
              <block lx="2.34999752" ly="6.58951187" lz="16.3749924" ux="2.39999676" uy="6.63951397" uz="16.8749962" index="999" material="3" look="0" up="3" color="1589d4"/>
          </item>
          <item parent="1234" index="5678">
              <block lx="2.34999752" ly="6.58951187" lz="16.3749924" ux="2.39999676" uy="6.63951397" uz="16.8749962" index="151" material="3" look="0" up="3" color="ffcd0000"/>
          </item>
          

          And the look-up table, below :

          
          •-------•---------•----------------•
          | Index |  Color  |    Old Color   |
          •-------•---------•----------------•
          |  151  | aabbcc  |  ( ffcd0000 )  |
          |  200  | FFFFFF  |  ( abcdef   )  |
          |  999  | 0062c8  |  ( 1589d4   )  |
          •-------•---------•----------------•
          

          Then, with the regex S/R :

          SEARCH (?-s)index="(?:(151)|(200)|(999))".*"\K[[:xdigit:]]+(?="/>)

          REPLACE (?{1}aabbcc)(?{2}FFFFFF)(?{3}0062c8)

          after clicking on the Replace All button, you’ll get :

          <item parent="9463" index="9476">
              <block lx="2.34999752" ly="6.58951187" lz="16.3749924" ux="2.39999676" uy="6.63951397" uz="16.8749962" index="151" material="3" look="0" up="3" color="aabbcc"/>
          </item>
          <item parent="8956" index="2458">
              <block lx="2.34999752" ly="6.58951187" lz="16.3749924" ux="2.39999676" uy="6.63951397" uz="16.8749962" index="200" material="3" look="0" up="3" color="FFFFFF"/>
          </item>
          <item parent="1268" index="6327">
              <block lx="2.34999752" ly="6.58951187" lz="16.3749924" ux="2.39999676" uy="6.63951397" uz="16.8749962" index="999" material="3" look="0" up="3" color="0062c8"/>
          </item>
          <item parent="1234" index="5678">
              <block lx="2.34999752" ly="6.58951187" lz="16.3749924" ux="2.39999676" uy="6.63951397" uz="16.8749962" index="151" material="3" look="0" up="3" color="aabbcc"/>
          </item>
          

          Best Regards,

          guy038

          1 Reply Last reply Reply Quote 2
          • Richard Brock Jr.R
            Richard Brock Jr.
            last edited by

            Thank you both very much.

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