Community

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

    Search in *.txt Files for a string that's not in and add it

    Help wanted · · · – – – · · ·
    3
    4
    56
    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.
    • Odyseus
      Odyseus last edited by

      Hello all,
      I am looking for a way to do the following in Notepad++ using the “Search in files”…

      1. search in all files of type *.txt for the term: Keyword=notIndexedvalue
      2. in the files where the term is not included, add it at the end of the file and save the file.

      Is this possible at all? I have not been able to find anything despite intensive search…
      It would be great if someone could help me there.
      Many thanks in advance.

      Greetings from Germany…

      1 Reply Last reply Reply Quote 0
      • Terry R
        Terry R last edited by

        @Odyseus said in Search in *.txt Files for a string that's not in and add it:

        Is this possible at all? I have not been able to find anything despite intensive search…

        Yes I do believe it’s possible, in fact I just tested it and it appears to work.

        What we use is the “alternation” option in regular expressions. That means we take 1 of 2 paths. We either find a file with the keyword, or the alternative is the file is checked, keyword not found and the additional text added to the end of the file.

        So the Find What is:(?s-i).*?Keyword=notIndexedvalue.*\K|(.*\K)
        Replace With:(?1Keyword=notIndexedvalue)

        With alternation, the regex tries to complete the find with the first option, which is a file with the keyword included. If it fails then it takes the second option which here it reads every character, then forgets them (\K) and then adds the keyword at the end of the file.

        I did note that in a test the keyword will be added to the end of the last line. Perhaps you wish it to be on a “new” line in which case the Replace field should be (?1\r\nKeyword=notIndexedvalue)

        See if that works for you.

        Terry

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

          Hi, @odyseus, @terry-r and All,

          Ah nice shot, Terry ! You’re using this powerful technique Not_This|Not_That|Go_Away|(We_Want_This), fully described in :

          https://www.rexegg.com/regex-best-trick.html

          Best Regards,

          guy038

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

            @Terry-R said in Search in *.txt Files for a string that's not in and add it:

            (?s-i).?Keyword=notIndexedvalue.\K|(.*\K)

            Hello Terry,
            thank you so much… Works very fine for me…

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