Community
    • Login

    Delete lines with a date older than a week old.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 2 Posters 480 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.
    • Dennis PratherD
      Dennis Prather
      last edited by Dennis Prather

      Fellow Notepad++ Users,

      Could you please help me with the following search-and-replace problem I am having?

      I want to delete all lines with dates older than a week old.

      ,0627,297,07/21/22 05:43 AM,322400-
      ,0712,19273,07/06/22 12:05 PM,713020-
      ,0713,2958,04/19/22 11:43 AM,714000-
      ,0714,12908,07/19/22 05:14 PM,732000-

      Here is how I would like the data to look.

      ,0627,297,07/21/22 05:43 AM,322400-
      ,0714,12908,07/19/22 05:14 PM,732000-

      To accomplish this, I have tried using the following Find/Replace expressions and settings.

      I have found no help from someone doing this.

      Thank you.

      Alan KilbornA 2 Replies Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @Dennis Prather
        last edited by

        @Dennis-Prather

        Is there any reason you just didn’t continue posting to THIS thread?

        1 Reply Last reply Reply Quote 0
        • Alan KilbornA Alan Kilborn referenced this topic on
        • Alan KilbornA
          Alan Kilborn @Dennis Prather
          last edited by

          @Dennis-Prather said in Delete lines with a date older than a week old.:

          I have tried using the following Find/Replace expressions and settings.

          It cannot be done this way, because how are you going to tell Find/Replace what “a week” means?

          And why did you say “following” when nothing follows?

          1 Reply Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn
            last edited by Alan Kilborn

            Here’s a simple PythonScript that seems to accomplish the goal:

            # -*- coding: utf-8 -*-
            from __future__ import print_function
            
            from Npp import *
            import datetime as dt
            import time
            
            #-------------------------------------------------------------------------------
            
            class T23284(object):
            
                def __init__(self):
                    date_current = dt.datetime(*time.localtime()[:6])
                    def replace_func(m):
                        date_in_question = dt.datetime(*time.strptime(m.group(1), '%m/%d/%y')[:6])
                        return '' if (date_current - date_in_question).days > 7 else m.group(0)
                    editor.rereplace(r'(?-s)^,\d*?,\d*?,(\d{2}/\d{2}/\d{2}).*(\R|\z)', replace_func)
            
            #-------------------------------------------------------------------------------
            
            if __name__ == '__main__': T23284()
            
            1 Reply Last reply Reply Quote 2
            • Dennis PratherD
              Dennis Prather
              last edited by

              Thank You ill give it a try.

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