Jump to content

Search for a string within a text file


Hadez
 Share

Recommended Posts

Iam able to search for a string within a text file, but the problem arrises when there is an unknown variable attached to the string im searching for. E.G.

I fileread Log.txt.

I need to search for the string 'Process Error/Cause/xxyyyy' ( where x are unknown letters and y are unknown numbers. 'Process Error/Cause/' is always present before the unknown characters)

This string could be contained anywhere within the txt file so i cant specify a line number to search.

Once the string is found, the string is written in a msgbox

How would i go about searching for this string?

Thanks for any help you can give.

Link to comment
Share on other sites

Iam able to search for a string within a text file, but the problem arrises when there is an unknown variable attached to the string im searching for. E.G.

I fileread Log.txt.

I need to search for the string 'Process Error/Cause/xxyyyy' ( where x are unknown letters and y are unknown numbers. 'Process Error/Cause/' is always present before the unknown characters)

This string could be contained anywhere within the txt file so i cant specify a line number to search.

Once the string is found, the string is written in a msgbox

How would i go about searching for this string?

Thanks for any help you can give.

$var = _StringBetween ("Process Error/Cause/","'")
if IsArray ($var) Then
While Not StringInStr(FileRead ("Log.txt"),$var)
Sleep(100)
Wend

dont think i missed anything. im not an expert.

Link to comment
Share on other sites

I think that would work if i knew what was before and after the string but unfortunately i dont. the line on the text file could be:

iiiiiiiiiiiiiiiffffff Process Error/Cause/as2455 aaaaaaaassasadasdasdfsasf or

hhhhhhhhhjjjjjjfuf Process Error/Cause/ds1234 dasdasdfaeffvasd or

aadf Process Error/Cause/hj4415 dsdsdasdasd

etc

i put the ' in the first post to show what the string was. '' are not a part of the string. thanks for taking the time ot reply though :graduated:

Link to comment
Share on other sites

$sStr = FileRead("somefullpath.txt")
$aSRE = StringRegExp($sStr, "(/i)(?m:^).+error/cause/(.+)(?:\v|$)+", 3)
If NOT @Error Then
    For $i = 0 To Ubound($aSRE) -1
        MsgBox(0, "Result " & $i+1), $aSRE[$i])
    Next
EndIf

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!"

Link to comment
Share on other sites

$nOffset = 1
$sStr = FileRead("C:\Documents and Settings\ST\Desktop\Log.txt")
$aSRE = StringRegExp($sStr,'Process Error/Cause/(.+)(?:\v|$)+', 1, $nOffset)

 ;StringRegExp("test",
If NOT @Error Then
    For $i = 0 To Ubound($aSRE) -1
    msgbox(0, "Process Error/Cause/" & $i, "Process Error/Cause/" & $aSRE[$i])

    Next
EndIf

so far that finds the string im looking for but also has all the end characters to the readline that i dont want. is there any way to trim the string so that only 6 characters are after 'Process Error/Cause/' ? E.g

The line on the text reads 'gggggggggg Process Error/Cause/sd0215 ooooooooooooooooo'

stringregexp gets it to 'sd0215 ooooooooooooooooo'

i only want the sd0215 (sd0215 is a variable) any ideas how to trim it?

Link to comment
Share on other sites

You don't need $nOffset. 1 is the default

$aSRE = StringRegExp($sStr,"(?i)Process Error/Cause/([a-z]{2}\d{4}).*(?:\v|$)+", 3)

Edit. Had the expression backwards

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!"

Link to comment
Share on other sites

You could also try this one

$aSRE = StringRegExp($sStr,"(?i)Process Error/Cause/([a-z]{2}\d+)\D.*(?:\v|$)+", 3)

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!"

Link to comment
Share on other sites

Glad to help.

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!"

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...