Jump to content

Delete line in file based on 2 conditions


Recommended Posts

Hello,

I got a .txt file that contains data (number of lines is dynamic) in the following format: "<md5 hash>|date time"

Example:

0x6FE88F555378C1249DA1D96FE3F117FA|2015/11/05 20:12:34
0x88FEA58B1F1017E4BEAC8C855999D1D5|2015/11/05 21:22:34
0x71BD989277F0D67B2E8F7BBB4644F047|2015/11/05 21:42:34
0x135F7153C9C7137BF1F7265247A74AB3|2015/11/05 22:12:34

 

I want to read each line from file, explode/split the string by the "|" separator and if more then "x" seconds passed since the line's date/time, I want to delete the line and proceed to evaluate next line until EOF.

Based on the examples and info I found on the this forum I put together the following code (which obviously is not working):

$file = FileOpen("log.txt", 0)
$text = FileRead($file)
While 1
    $lines = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $l =  StringSplit($lines, "|")
    $time_d = _DateDiff('s', $l[2], _NowCalc())
    if $time_d > 600 Then
        $text = StringReplace($text, $lines, @CRLF)
        FileWrite($file,$text)
    EndIf
WEnd
FileClose($file)

In PHP I would do the above task with the code below:

$handle = fopen($scanf, "r");
            if ($handle) {
                while (($line = fgets($handle)) !== false) {;
                    $vals = explode("|",$line);
                    $del_line = trim($vals[4]); //date
                    if ($del_line < time()-86400){ 
                        $contents = file_get_contents($scanf);
                        $contents = str_replace($line, '', $contents);
                        file_put_contents($scanf, $contents);
                    }
                }
            }   
            fclose($handle);

Can anyone help me find an working Autoit solution?

Thank you

Link to comment
Share on other sites

#include <Date.au3>

$now = _NowCalc()
$text = FileRead("log.txt")
$file = FileOpen("log.txt")

While 1
    $lines = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $l =  StringSplit($lines, "|")
    $time_d = _DateDiff('s', $l[2], $now)
    ; msgbox(0,"", $time_d)
    if $time_d > 3600 Then
        $text = StringReplace($text, $lines & @crlf, "")
    EndIf
WEnd

$file = FileOpen("log.txt", 2)
FileWrite($file, $text)
FileClose($file)

:)

Link to comment
Share on other sites

Here is another method.

Note :
The StringRegExpReplace function captures three groups per file line:-
${1} - contains from the beginning of the line to, and including, the vertical bar,
${2} - contains from the vertical bar the end of the line (the date & time), and,
${3} - contains the line feed at the end of the line (if it exists).

Then, in the 'replace' parameter of the StringRegExpReplace function,  a conditional operator is use with if the date difference as the condition parameter.   Conditional operator. e.g.  $condition ? $expression1 : $expression2  

#include <Date.au3>
#include <FileConstants.au3>

Local $fileName = "log.txt"

Local $s = '"' & StringRegExpReplace(FileRead($fileName), "([^|]+\|)(.+)(\R*)", '"&((_DateDiff("s","${2}", _NowCalc()) > 600) ? "" : "${1}${2}${3}")&"') & '"'
;ConsoleWrite($s & @LF)
;ConsoleWrite("-----------------------" & @LF)

Local $text = Execute($s)
;ConsoleWrite($text & @LF)

; Write to file
Local $hFileOpen = FileOpen($fileName, $FO_OVERWRITE)
FileWrite($hFileOpen, $text)
FileClose($hFileOpen)
ShellExecute($fileName)

 

Link to comment
Share on other sites

#include <Date.au3>

$now = _NowCalc()
$text = FileRead("log.txt")
$file = FileOpen("log.txt")

While 1
    $lines = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $l =  StringSplit($lines, "|")
    $time_d = _DateDiff('s', $l[2], $now)
    ; msgbox(0,"", $time_d)
    if $time_d > 3600 Then
        $text = StringReplace($text, $lines & @crlf, "")
    EndIf
WEnd

$file = FileOpen("log.txt", 2)
FileWrite($file, $text)
FileClose($file)

:)

@mikell thank you.

I noticed the above code does not always perform as expected. For some reason it does not delete the last line in the file if the time condition is met.

for example:

0x551849DDECF6296BE48EC5D8B6A664EF|2015/11/05 22:12:34 (past - delete)
0xDC16555CF6383AA0A134C6F3A8692D33|2015/11/05 06:10:34 (past - delete)
0xB8B383146356B6D24F0088CB10FF2A2D|2015/11/05 22:12:34 (past - delete)
0x6A9558A6DD80BE0F460C730458F3A9CD|2015/11/06 22:12:34 (present - do not delete)
0x850C2E164B00333E39CD4EC90D0AD1A4|2015/11/06 06:10:34 (present - do not delete)
0x570352966B1F47FD91C67C374A15E5BC|2015/11/05 21:12:34 (past - delete)

running the script on the above file lines will not delete the last line:
0x570352966B1F47FD91C67C374A15E5BC|2015/11/05 21:12:34 (past - delete)

even if all lines in the file are (past-delete) it will still keep the last line.

This happens only if there is no blank (empty) line at the end of file. If i insert a blank line, the script performs as expected

 

@Malkey, your code works perfectly no matter where and how the blank lines are found in the file. Thank you very much.

Here is another method.

Note :
The StringRegExpReplace function captures three groups per file line:-
${1} - contains from the beginning of the line to, and including, the vertical bar,
${2} - contains from the vertical bar the end of the line (the date & time), and,
${3} - contains the line feed at the end of the line (if it exists).

Then, in the 'replace' parameter of the StringRegExpReplace function,  a conditional operator is use with if the date difference as the condition parameter.   Conditional operator. e.g.  $condition ? $expression1 : $expression2  

#include <Date.au3>
#include <FileConstants.au3>

Local $fileName = "log.txt"

Local $s = '"' & StringRegExpReplace(FileRead($fileName), "([^|]+\|)(.+)(\R*)", '"&((_DateDiff("s","${2}", _NowCalc()) > 600) ? "" : "${1}${2}${3}")&"') & '"'
;ConsoleWrite($s & @LF)
;ConsoleWrite("-----------------------" & @LF)

Local $text = Execute($s)
;ConsoleWrite($text & @LF)

; Write to file
Local $hFileOpen = FileOpen($fileName, $FO_OVERWRITE)
FileWrite($hFileOpen, $text)
FileClose($hFileOpen)
ShellExecute($fileName)

 

Thank you both for the help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...