Jump to content

Recommended Posts

Posted (edited)

Hi all!

I have a file.txt with all info about process running and I want to print out only lines / rows that have a value bigger then 3600 under "Time"

So for example if I have the attacched file, I have to print the line or lines:

29 DIA 11440 Run yes no 1 5699 SAPDBKDF 150 U818BARONC

is this easy to do? I think I should use _FileReadToArray and then loop something but don't know what.

tnx for reply :bye:

file.txt

Edited by superbosu
Posted

Looping though the array is a good idea!

Make sure that you translate the "number" derived from the text file to a real number (function Number). That's because a FileRead returns a string.

My UDFs and Tutorials:

  Reveal hidden contents

 

  • Moderators
Posted

superbosu,

This will write the lines with the "Run" value > 3600 to the SciTE console - up to you to get them printed: ;)

#include <File.au3>

Global $aLines
_FileReadToArray("file.txt", $aLines)

For $i = 6 To $aLines[0]
    $aRet = StringRegExp($aLines[$i], "(\d+)", 3)
    If Number($aRet[1]) > 3600 Then
        ConsoleWrite($aLines[$i] & @CRLF)
    EndIf
Next

Please ask if you have any questions. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

Hi Melba23 and tnx for the reply. :)

I tried your code with the file but the output was the same as the original file.txt without the top header lines:

Workprocess Table Wed Jan 23 16:03:53 2013

=================

No Type Pid Status Cause Start Rstr Err Time Program Cl User SemR SemL

------------------------------------------------------------------------------------------

right?

I think something is missing... any ideas?

  • Moderators
Posted

suprebosu,

My most abject apologies - I was looking at the PID column! :>

Just change $aRet[1]) to $aRet[3]). :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

Here another similar way:

Global $aFilter = StringRegExp(FileRead(@ScriptDir & "\File.txt"), "\d+\h+\w+\h+\d+\h+\w+\h+\w+\h+\w+\h+\d+\h+\d+.*", 3)
If @error Then Exit
Global $i, $a, $sLines
For $i = 0 To UBound($aFilter) - 1
    $a = StringRegExp($aFilter[$i], "\d+\h+\w+\h+\d+\h+\w+\h+\w+\h+\w+\h+\d+\h+(\d+).*", 3)
    If @error Then ContinueLoop
    If $a[0] > 3600 Then $sLines &= $aFilter[$i] & @LF
Next
MsgBox(0, "Test", $sLines)

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted (edited)

Depending on the size of your file this could be faster

Local $file = FileOpen("file.txt", 0)
Local $fileData = FileRead($file)
FileClose($file)

$array=stringregexp($filedata, "\r\n(.{45})([3-9].{4})(.+\r\n)",4)
consolewrite("Length of file: " & stringlen($filedata) &@CRlf)
consolewrite("Analyzing " & UBound($array) & " lines" &@CRlf )

For $i = 0 To UBound($array) - 1
Local $match = $array[$i]
;~   For $j = 0 To UBound($match) - 1
;~  consolewrite( "Match " & $i & ',' & $j & ":" & $match[$j] & @CRlf)
;~   Next
if number($match[2])>3600 then
  consolewrite( "Match :" & $match[0] & @CRlf)
endif
Next
Edited by junkew
Posted (edited)

When testing, this example would return the lines with 4599, 5699, and 3601 in the "Time" column.

And this example would not return the lines with 2601, 3599, or 3600 in the "Time" column.

#include <Array.au3>

$aArray = StringRegExp(FileRead("file1.txt"), "((?:[^\s]+\h+){7}(?:[3-9][6-9][0-9][1-9]|[3-9][7-9][0-9][0-9]|[4-9][0-9][0-9][0-9])\h?[^\v]*\v*)", 3) ; )
ConsoleWrite(FileRead("file1.txt") & @LF)
_ArrayDisplay($aArray)

; Use if there are greater than 4 digit numbers expected.
$aArray = StringRegExp(FileRead("file1.txt"), "((?:[^\s]+\h+){7}(?:[3-9][6-9][0-9][1-9]|[3-9][7-9][0-9][0-9]|[4-9][0-9][0-9][0-9]|[1-9][0-9]{4,})\h?[^\v]*\v*)", 3)
_ArrayDisplay($aArray)

Edit: "|[3-9][7-9][0-9][0-9]" added to regular expression pattern because 3700, 3800, and 3900 were not being captured.

And added an adjusted JohnQSmith's example of post #12.

Edited by Malkey
Posted

  On 1/23/2013 at 11:37 PM, 'Malkey said:

When testing, this example would return the lines with 4599, 5699, and 3601 in the "Time" column.

And this example would not return the lines with 2601, 3599, or 3600 in the "Time" column.

#include <Array.au3>

$aArray = StringRegExp(FileRead("file.txt"), "((?:[^\s]+\h+){7}(?:[3-9][6-9][0-9][1-9]|[4-9][0-9][0-9][0-9])\h?[^\v]*\v*)", 3) ; )

_ArrayDisplay($aArray)

Excellent solution Malkey! :thumbsup:

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted (edited)

  On 1/23/2013 at 11:37 PM, 'Malkey said:

#include <Array.au3>

$aArray = StringRegExp(FileRead("file.txt"), "((?:[^\s]+\h+){7}(?:[3-9][6-9][0-9][1-9]|[4-9][0-9][0-9][0-9])\h?[^\v]*\v*)", 3) ; )

_ArrayDisplay($aArray)

What if some future value is greater than 4 digits? I'd recommend expanding it to...

$aArray = StringRegExp(FileRead("file.txt"), "((?:[^\s]+\h+){7}(?:[3-9][6-9][0-9][1-9]|[4-9][0-9][0-9][0-9]|[1-9][0-9]{4,})\h?[^\v]*\v*)", 3)
Edited by JohnQSmith

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

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
  • Recently Browsing   0 members

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