• Login
Community
  • Login

Delete lines with a date older than a week old.

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
5 Posts 2 Posters 648 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.
  • D
    Dennis Prather
    last edited by Dennis Prather Jul 22, 2022, 3:54 PM Jul 22, 2022, 3:54 PM

    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.

    A 2 Replies Last reply Jul 22, 2022, 5:19 PM Reply Quote 0
    • A
      Alan Kilborn @Dennis Prather
      last edited by Jul 22, 2022, 5:19 PM

      @Dennis-Prather

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

      1 Reply Last reply Reply Quote 0
      • A Alan Kilborn referenced this topic on Jul 22, 2022, 5:19 PM
      • A
        Alan Kilborn @Dennis Prather
        last edited by Jul 22, 2022, 5:21 PM

        @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
        • A
          Alan Kilborn
          last edited by Alan Kilborn Jul 22, 2022, 5:43 PM Jul 22, 2022, 5:43 PM

          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
          • D
            Dennis Prather
            last edited by Jul 27, 2022, 10:08 PM

            Thank You ill give it a try.

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