Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    search and replace/add

    Help wanted · · · – – – · · ·
    2
    2
    2423
    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.
    • vk_notepad_123
      vk_notepad_123 last edited by

      I want to modify the 2nd line and add a 3rd line in the following lines which lets repeats lot of times with different IP:

      abc 11.1.1.1 xyz 100
      abc 11.1.1.1 update-source efg2021
      abc 12.1.1.1 xyz 100
      abc 12.1.1.1 update-source efg2021

      to: (the replace should not care for the IP and update the 2nd line/add every 3rd line

      abc 11.1.1.1 xyz 100
      update-source efg2021
      abcd-efg-ijk
      abc 12.1.1.1 xyz 100
      update-source efg2021
      abcd-efg-ijk

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

        Hello, vk_Notepad_123,

        If I, fully, understand you :

        • For each block of two lines, below, with an identical IP address nnn.nnn.nnn.nnn

          Random text1 nnn.nnn.nnn.nnn Random text2
          Random text3 nnn.nnn.nnn.nnn Random text4

        You would like to obtain, after replacement, the text below :

        Random text1 nnn.nnn.nnn.nnn Random text2
        Random text4
        abcd-efg-ijk
        

        If so, use the following S/R, in regular expression search mode :

        SEARCH ^.*((?:\d{1,3}\.){3}\d{1,3}).*\R\K.*\1\h*(.+)

        REPLACE \2\r\nabcd-efg-ijk

        IMPORTANT : Due to the \K form, you must click, exclusively, on the Replace All button ( DON’T use the step-by-step replacement, with the Replace button )

        NOTES :

        • This regex will work even if Random text1 and/or Random text2 and/or Random text3 does not exist

        • This regex will work even if no blank character ( space or tabulation ), exists between the IP address and Random text4, in second line

        • The part ((?:\d{1,3}\.){3}\d{1,3}) looks for four integers, from 1 to 3 digits, separated by the dot symbol. However, consider that this regex does not verify the integrity of the IP address. Indeed, this syntax would match the string 999.999.999.999, which is not, obviously, a valid IP address !

        • The IP address, preceded and followed by any range, even null, of characters ( .* ) and ended by its EOL characters ( \R ) is, then, forgotten by the regex engine, due to the \K syntax

        • Now, it tries to match the second line : any range even null, of characters, followed by the SAME IP address, stored as group 1 ( \1 ), followed by possible horizontal blank characters ( \h* )

        • As the previous syntax (?:\d{1,3}\.){3} defines a non-capturing group, repeated 3 times, the remainder of line 2, Random text4 (.+) ) is, therefore, stored as group2

        • In the replacement part, this second line is re-written with Random text4 ( \2 ), only, followed by a line break ( \r\n ), and, finally, followed by the simple string abcd-efg-ijk

        Best Regards,

        guy038

        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        Copyright © 2014 NodeBB Forums | Contributors