Sign in to follow this  
Followers 0
ksmith247

Search for particular text in a text file

13 posts in this topic

Perhaps I am overlooking somehthing obvious but I need to search a text file for a particular character string.

Here is the log file:

Category: 0
 Computer Name: VPCT-XPSP2-1
 Event Code: 1001
 Message: Checking file system on C:
 The type of the file system is NTFS.
 
 
 A disk check has been scheduled.
 Windows will now check the disk.                        
 Cleaning up minor inconsistencies on the drive.
 Cleaning up 70 unused index entries from index $SII of file 0x9.
 Cleaning up 70 unused index entries from index $SDH of file 0x9.
 Cleaning up 70 unused security descriptors.
 
   16763795 KB total disk space.
    5536044 KB in 16560 files.
       4868 KB in 2056 indexes.
          0 KB in bad sectors.
      86507 KB in use by the system.
      65536 KB occupied by the log file.
   11136376 KB available on disk.
 
       4096 bytes in each allocation unit.
    4190948 total allocation units on disk.
    2784094 allocation units available on disk.
 
 Internal Info:
 a0 4d 00 00 c3 48 00 00 6f 5c 00 00 00 00 00 00  .M...H..o\......
 4b 00 00 00 01 00 00 00 49 02 00 00 00 00 00 00  K.......I.......
 d0 7f c3 02 00 00 00 00 10 f2 e9 0d 00 00 00 00  ................
 50 f8 49 03 00 00 00 00 00 00 00 00 00 00 00 00  P.I.............
 00 00 00 00 00 00 00 00 40 c1 6f 1c 00 00 00 00  ........@.o.....
 99 9e 36 00 00 00 00 00 00 39 07 00 b0 40 00 00  ..6......9...@..
 00 00 00 00 00 b0 e4 51 01 00 00 00 08 08 00 00  .......Q........
 
 Windows has finished checking your disk.
 Please wait while your computer restarts.
 
 
 Record Number: 10
 Source Name: Winlogon
 Time Written: 20071204145427.000000-300
 Event Type: information
 User:

This is a dump of the application event log. I need to read the Time Written line then pull out the timestamp. I'm using StringTrim to get the timestamp to a more readable format but I can't seem to figure out how to search the file for the line. FileReadLine works but it seems that I have to know the line number and it could change from logfile to logfile. StringInStr looks close but not sure if that is the way to go.


Support bacteria; it's the only culture most people have.LxP's Learning to Script with AutoIt 3 - Excellent starting placeVolly's Links Page - Links to cool and useful scriptsAutoIt Wrappers - Valuater's AutoIt Wrappers post. Lots of good stuff.Support AutoIt - Make a donation here; I did.[size="2"]#include <Guinness.pint>[/size]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

#include <file.au3>
#include <array.au3>

Dim $logArray

_FileReadToArray("text.log", $logArray)
If @ERROR Then
    MsgBox(0,"","Error opening file")
    Exit
EndIf

$result = _ArraySearch ( $logArray, "Time Written:")
If $result = -1 Then
    MsgBox(0,"", "Timestamp not found")
    Exit
EndIf

$timestamp = StringTrimLeft($logArray[$result], 14)

MsgBox(0,"",$timestamp)

EDIT: Forgot underscore in front of ArraySearch

EDIT: _FileListToArray changed to _FileReadToArray

Edited by weaponx

Share this post


Link to post
Share on other sites

#include <file.au3>
#include <array.au3>

Dim $logArray

_FileListToArray("text.log", $logArray)
If @ERROR Then
    MsgBox(0,"","Error opening file")
    Exit
EndIf

$result = _ArraySearch ( $logArray, "Time Written:")
If $result = -1 Then
    MsgBox(0,"", "Timestamp not found")
    Exit
EndIf

$timestamp = StringTrimLeft($logArray[$result], 14)

MsgBox(0,"",$timestamp)

EDIT: Forgot underscore in front of ArraySearch

Ok, it keeps saying Error opening file. The file is there. I fell that there is something simple that I am overlooking here.

Support bacteria; it's the only culture most people have.LxP's Learning to Script with AutoIt 3 - Excellent starting placeVolly's Links Page - Links to cool and useful scriptsAutoIt Wrappers - Valuater's AutoIt Wrappers post. Lots of good stuff.Support AutoIt - Make a donation here; I did.[size="2"]#include <Guinness.pint>[/size]

Share this post


Link to post
Share on other sites

Should I assume that you changed the filename to match yours?

Share this post


Link to post
Share on other sites

Ok, it keeps saying Error opening file. The file is there. I fell that there is something simple that I am overlooking here.

First make sure that the path is fully qualified.

Looking at that code I think the wrong function was used anyway. You already know what file it is so you don't need _FileListToArray()

You need

_FileReadToArray ()

In the help file under user defined functions>>file management.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Oops my fault. I only wrote it in the bowser so I never tried running it.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Well I'm doing the same here but this should work.

#include <file.au3>
#include <array.au3>

Dim $logArray
$log_file = "<path>\text.log";;Replace <path> with the actual path of the folder containing the file
If FileExists($log_file) Then
_FileReadToArray($log_File, $logArray)

$result = _ArraySearch ( $logArray, "Time Written:")
If $result = -1 Then
    MsgBox(0,"", "Timestamp not found")
    Exit
EndIf

$timestamp = StringTrimLeft($logArray[$result], 14)

MsgBox(0,"",$timestamp)
Else
  MsgBox(4096,"No File", "The file " & $log_file & " does not exist")
  Exit
EndIf

Edit: Forgot the code tags

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Should I assume that you changed the filename to match yours?

I realize around here that one can never be too careful and should assume nothing; but yes.

First make sure that the path is fully qualified.

Looking at that code I think the wrong function was used anyway. You already know what file it is so you don't need _FileListToArray()

You need

_FileReadToArray ()

Thank you too GEOSoft.


Support bacteria; it's the only culture most people have.LxP's Learning to Script with AutoIt 3 - Excellent starting placeVolly's Links Page - Links to cool and useful scriptsAutoIt Wrappers - Valuater's AutoIt Wrappers post. Lots of good stuff.Support AutoIt - Make a donation here; I did.[size="2"]#include <Guinness.pint>[/size]

Share this post


Link to post
Share on other sites

I realize around here that one can never be too careful and should assume nothing; but yes.

Thank you too GEOSoft.

NP

Let us know if it works.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Getting closer. Use of _ArrayDisplay($logArray) lets me know that the file is being read but the search is coming up empty unless I use the entire line.

$result = _ArraySearch ( $logArray, "Time Written:")oÝ÷ Ø:²z-Âä½êÚºÚ"µÍÌÍÜÝ[HÐ^TÙXÚ
    ÌÍÛÙÐ^K    ][ÝÕ[YHÜ][
ÌL
M
MËLÌ  ][ÝÊ

Does.

Obviously the timestamp of the log will change as this runs on different machines. This is my first foray into arrays. Not really sure what I am doing.


Support bacteria; it's the only culture most people have.LxP's Learning to Script with AutoIt 3 - Excellent starting placeVolly's Links Page - Links to cool and useful scriptsAutoIt Wrappers - Valuater's AutoIt Wrappers post. Lots of good stuff.Support AutoIt - Make a donation here; I did.[size="2"]#include <Guinness.pint>[/size]

Share this post


Link to post
Share on other sites

You need to do a partial search. I guess I thought that was the default action.

Change this line:

$result = _ArraySearch ( $logArray, "Time Written:")

to

$result = _ArraySearch ( $logArray, "Time Written:", 0, 0, 0, true)

Share this post


Link to post
Share on other sites

I should have caught that since I've had problems with it before.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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