Sign in to follow this  
Followers 0
robhunter

Find multiple strings, copy that lines and remove the rest

5 posts in this topic

#1 ·  Posted (edited)

Hi

I need to search two strings, copy the lines between both (including them) and remove the rest...

This is the txt file I am using

01.03   0   0   0   0   0
01.02   1   1   0   0   0
01.01   1   1   0   0   0
01.00   1   0   1   0   0
00.59   2   1   1   0   0
00.58   2   0   2   0   0
00.57   0   0   0   0   0
00.56   2   2   0   0   0
00.55   1   1   0   0   0
00.54   0   0   0   0   0
00.53   0   0   0   0   0
00.52   0   0   0   0   0
00.51   1   1   0   0   0
00.50   0   0   0   0   0
00.49   0   0   0   0   0
00.48   0   0   0   0   0
00.47   0   0   0   0   0
00.46   1   1   0   0   0
00.45   0   0   0   0   0
00.44   0   0   0   0   0
00.43   1   1   0   0   0
00.42   0   0   0   0   0
00.41   1   1   0   0   0
00.40   0   0   0   0   0
00.39   0   0   0   0   0
00.38   0   0   0   0   0
00.37   0   0   0   0   0
00.36   0   0   0   0   0
00.35   0   0   0   0   0

So, I have defined some variables

$min = @min

If $min>="00" and $min<="14" Then
    $inicio =   ".46"
    $final  =   ".00"
ElseIf $min>="15" and $min<="29" Then
    $inicio =   ".01"
    $final  =   ".15"
ElseIf $min>="30" and $min<="44" Then
    $inicio =   ".16"
    $final  =   ".30"
ElseIf $min>="45" and $min<="59" Then
    $inicio =   ".31"
    $final  =   ".45"
EndIf

So,i need to find $inicio and $final, and delete what is not on that range.

I was using this

;BORRAMOS HACIA ARRIBA
Send("^f")
Send($final)
Send("{ENTER}")
Send("{ESCAPE}")
Send("{HOME}")
Send("{SHIFTDOWN}")
Send("{UP 40}")
Send("{SHIFTUP}")
Send("{BACKSPACE}")

;BORRAMOS HACIA ABAJO
Send("^f")
Send($inicio)
Send("{ENTER}")
Send("{ESCAPE}")
Send("{END}")
Send("{SHIFTDOWN}")
Send("{DOWN 40}")
Send("{SHIFTUP}")
Send("{BACKSPACE}")

This is being used with a NOTEPAD.exe open window.. I want to use FileOpen, FileWrite and that stuff so I can have a hidden window doing its things

But this code slows the script and uses notepad window so.. I need something fast,almost instant and invisible...

Can someone help me?

Thanks!

Edited by robhunter

Share this post


Link to post
Share on other sites



Just an example:

#include <file.au3>

Global $aFile
_FileReadToArray("your_file.txt", $aFile)
$min = @MIN

Select
    Case $min >= "00" And $min <= "14"
        _Go(46, 00)
    Case $min >= "15" And $min <= "29"
        _Go(01, 15)
    Case $min >= "30" And $min <= "44"
        _Go(16, 30)
    Case $min >= "45" And $min <= "59"
        _Go(31, 45)
EndSelect

Func _Go($start, $end)
    For $i = 1 To $aFile[0]
        $atemp = StringRegExp($aFile[$i], "\d{2}\.(\d{2})\s.+", 1)
        If $end = 00 Then
            If $atemp[0] >= $start And $atemp[0] <= 59 Or $atemp[0] = $end Then
                ConsoleWrite($aFile[$i] & @CRLF)
            EndIf
        Else
            If $atemp[0] >= $start And $atemp[0] <= $end Then
                ConsoleWrite($aFile[$i] & @CRLF)
            EndIf
        EndIf
    Next
EndFunc   ;==>_Go

The part to delete the lines or create a new file with only the lines you need is up to you. Above example will only write the lines into the console.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks, I'll try it!

Edited by robhunter

Share this post


Link to post
Share on other sites

Thanks, I'll try it!

I can't get it work...

I'm using this instead of ConsoleWrite

$fAprobadas =   FileOpen("E:\PAS_15_CAHOOT_APROBADAS.txt", 2)
_FileWriteFromArray($fAprobadas, $aFile, 1, $i)
FileClose($fAprobadas)

AND

$fAprobadas =   FileOpen("E:\PAS_15_CAHOOT_APROBADAS.txt", 2)
FileWrite($fAprobadas, $aFile[$i] & @CRLF
FileClose($fAprobadas)

But it doesnt work, it fills the .TXT file with the whole Array (the first one we get before we RegEx it)...

If i do

MsgBox(0,"",$aFile[$i] & @CRLF)

It shows me what I want, the data between timestamps and rejects the lines I don't want... So why it works with MsgBox but not with FileWrite or _FileWriteFromArray?

:huh2:

Share this post


Link to post
Share on other sites

Solved! I just export to another file with FileWriteToLine and it works

Share this post


Link to post
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
Sign in to follow this  
Followers 0