• Login
Community
  • Login

Fin/Replace in Determined Lines

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
5 Posts 4 Posters 3.8k 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.
  • F
    Fabrizio Ferraro
    last edited by Mar 10, 2016, 9:56 AM

    Hi, I need a way to find/replace text ONLY in lines that start for determined string.

    Ex.

    key= bla bla
    value= bla bla

    Replace “bla” in “blue” ONLY in strings that start for “value=”

    Can anyone help me?

    Thank you.

    C 1 Reply Last reply Mar 10, 2016, 5:50 PM Reply Quote 0
    • A
      Allan Biggs
      last edited by Mar 10, 2016, 4:26 PM

      in the search box
      \nvalue=

      and select radio button

      extended \n,\r

      worked for me - this was a DEC EVE feature great to see it in here

      A 1 Reply Last reply Mar 10, 2016, 5:18 PM Reply Quote 0
      • A
        Allan Biggs @Allan Biggs
        last edited by Mar 10, 2016, 5:18 PM

        @Allan-Biggs said:

        in the search box
        \nvalue=

        and select radio button

        extended \n,\r

        worked for me - this was a DEC EVE feature great to see it in here

        Oh just read your post properly - guess that’s not what you want.

        On Unix you could use grep etc

        1 Reply Last reply Reply Quote 0
        • C
          Claudia Frank @Fabrizio Ferraro
          last edited by Mar 10, 2016, 5:50 PM

          Hello @Fabrizio-Ferraro,

          sounds like a regex topic but solution depends on how line exactly looks.
          E.g. from regex point of view it is different if line is value=bla or value = bla.
          So without knowing how the lines are exactly we can only provide a roughly answer like mark regular expression in search mode and search for
          (value=)bla bla and replace with \1blue.

          Cheers
          Claudia

          1 Reply Last reply Reply Quote 0
          • G
            guy038
            last edited by guy038 Mar 27, 2016, 11:59 PM Mar 27, 2016, 2:20 PM

            Fabrizio and All,

            From your post, I tried to find a generic way to achieve this kind of question, with regexes ! Please, just consider my solution as, only, one, among all the possible solutions !

            The Fabrizio’s goal is to perform a Search/Replacement on a line, ONLY IF a SPECIFIC condition is true on that particular line

            I would use the general regex syntax, below :

            (Regex_Condition)[^@\r\n]*$|(Search_Regex)(?=.*@)|@
            
            (?1$0@)(?2Replacement_Regex)
            

            IMPORTANT :

            • Go back to the very beginning of your current file

            • In the Replace dialog, check the Regular search mode

            • Check, preferably, the Match case option

            • Click TWICE on the Replace All button

            NOTES :

            • The arobase character @, is, simply, any single character, which DOES NOT exist in your current file. and preferably, NOT a meta-character of regular expressions. So, according to your file’s contents, you can choose, for instance, the ! character, the # character, the % character, the & character, the = character, the _ character, …

            • Regex_Condition represents the regex that satisfies the specific condition, which allows the associated Search/Replacement. Of course, Regex_Condition may be, also, a simple literal string, even a single character !

            • Search_Regex represents, of course, the area of text, which will be replaced. As above, Search_Regex may be, only, a simple literal string, even a single character !

            • Replacement_Regex is, obviously, the regex to generate the text that will replace the area matched by the Search_Regex


            Below, I’ll expose 3 different examples ( The Fabrizio’s one and two others )

            Regex_Condition :   ^Value=                     Search_Regex :   \bbla\b               Replacement_Regex :   blue 
            
            Regex_Condition :   \b\d\d-\d\d-\d\d\b          Search_Regex :   [a-z]                 Replacement_Regex :   \u\2 
            
            Regex_Condition :   \d$                         Search_Regex :   [[:alpha:]]+          Replacement_Regex :   <\2> 
            
            • In the first case, we’ll replace the lowercase word bla, with the word blue, ONLY on lines beginning with the exact string Value=
              ( Fabrizio’s problem )

            • In the second case, we’ll replace any lowercase letter, with the same uppercase letter, ONLY on lines that contain a date, of the general form xx-xx-xx, at any position of the line

            • In the third case, we’ll replace any set of uppercase or lowercase letters, with the same set, surrounded by the two angle brackets, ONLY, if the current line ends with a digit


            To see the results of each example, I’ll use the SAME 10 lines test example, below :

            Value= bla bla testblatest bla
            That's only "bla bla" speech
            My NEXT bithday falls on 19-05-16
            Every 25-12, Chistmas is celebrated
            Value= Test of bla and BLA
            Christmas is celebrated each 25-12
            On the 01-01-17, we'll wish a Good New Year
            Don't change the word "bla", in that TEST line no 8
            01-01-00 was the first day OF THAT century
            Value=     bla blabla bla
            

            So the first example :

            (^Value=)[^@\r\n]*$|(\bbla\b)(?=.*@)|@
            
            (?1$0@)(?2blue)
            

            would give the changed text ( Lines 1, 5 and 10 ), below :

            Value= blue blue testblatest blue
            That's only "bla bla" speech
            My NEXT bithday falls on 19-05-16
            Every 25-12, Chistmas is celebrated
            Value= Test of blue and BLA
            Christmas is celebrated each 25-12
            On the 01-01-17, we'll wish a Good New Year
            Don't change the word "bla", in that TEST line no 8
            01-01-00 was the first day OF THAT century
            Value=     blue blabla blue
            

            The second example :

            (\b\d\d-\d\d-\d\d\b)[^@\r\n]*$|([a-z])(?=.*@)|@
            
            (?1$0@)(?2\u\2)
            

            would give the changed text ( Lines 3, 7 and 9 ), below :

            Value= bla bla testblatest bla
            That's only "bla bla" speech
            MY NEXT BITHDAY FALLS ON 19-05-16
            Every 25-12, Chistmas is celebrated
            Value= Test of bla and BLA
            Christmas is celebrated each 25-12
            ON THE 01-01-17, WE'LL WISH A GOOD NEW YEAR
            Don't change the word "bla", in that TEST line no 8
            01-01-00 WAS THE FIRST DAY OF THAT CENTURY
            Value=     bla blabla bla
            

            Finally, the third example :

            (\d$)[^@\r\n]*$|([[:alpha:]]+)(?=.*@)|@
            
            (?1$0@)(?2<\2>)
            

            would give the changed text ( Lines 3, 6 and 8 ), below :

            Value= bla bla testblatest bla
            That's only "bla bla" speech
            <My> <NEXT> <bithday> <falls> <on> 19-05-16
            Every 25-12, Chistmas is celebrated
            Value= Test of bla and BLA
            <Christmas> <is> <celebrated> <each> 25-12
            On the 01-01-17, we'll wish a Good New Year
            <Don>'<t> <change> <the> <word> "<bla>", <in> <that> <TEST> <line> <no> 8
            01-01-00 was the first day OF THAT century
            Value=     bla blabla bla
            

            NOTES :

            • Throughout these 3 examples, the lines 2 and 4, which do not begin with the string Value= nor end with a digit and which do not contain any date, remain UNCHANGED

            • For a correct behaviour of the proposed regexes, don’t forget to click, TWICE, on the Replace All button

            Best Regards,

            guy038

            P.S. :

            The underlined idea, about these general regexes, below :

            (Regex_Condition)[^@\r\n]*$|(Search_Regex)(?=.*@)|@
            
            (?1$0@)(?2Replacement_Regex)
            

            is :

            • Add a specific character ( @, or else, not used in current file ) at the end of EACH line that satisfies the Regex_Condition ( 1st S/R )

            • Perform the wanted S/R, ONLY on the lines that end with the @ character ( or else ) and finally delete that extra character, at the end of each concerned line ( 2nd S/R )

            As usual, I could give some extra information about these regexes, if needed !

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