Jump to content

Find line in .dat file from a string search


Fukima
 Share

Recommended Posts

Hey guys, 

 

I am having a really hard time trying to wrap my head around this concept. I am trying to find part of a string in a .dat file so it can read the rest of the line for me. 

For example if I am looking "autoitstories= * " I need the script to tell me what is the value of " * " 

Or just read the whole line for me

 

Any ideas how I should do this? 

Thank you very much

Edited by Tony27

[spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler]

Never compromise even in the face of Armageddon

 

 

[/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler]

Link to comment
Share on other sites

  • Moderators

Do you have an example .dat file?

You could.

1.  FileReadToArray

2.  Loop through each element of the array

3.  Search each element/line with a string function (such as StringInStr)

Or

1.  StringRegExp(FileRead("file.dat"), "(?im)autoitstories=s*(.*?)", 3)

2.  Each element should contain your value, assuming "= *" is not literally that, and " *" represents the data you want.

 

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

 
SmOke_N,
 

Thanks for the prompt response, let me show what I got so far. The script needs to read me the whole line or show me a specific passage of the line.

It will find the line from searching another string: 

 

 

Func CheckEmp($activeStatus)
   ; <AREA data_type="49" option="2" include_blobs="0" include_invn_base_item="1"
;$ActiveStr = "active=" & chr(34) & "1" & chr(34) & " path_in=" & chr(34) & chr(34)
;$EmployeeIN = "INPARTIALRPROLICENSE.XML"
   ;$InventoryDIF = "INPARTIALRPROLICENSE.XML"
 
Local $filename = "c:usersantoniodesktoplog.dat"
;if $activeStatus = 1 Then
; $find = $EmployeeIN
; $replace = $EmployeeIN
;EndIf
 
 
Local $retval = StringRegExp(FileRead($filename), "C:ECMPOLLINGs=s*(.*?)", 3)
If $retval = -1 Then
MsgBox(0, "ERROR", "The pattern could not be replaced in file: " & $filename & " Error: " & @error)
;Exit
Else
MsgBox(0, "INFO", "Found " & $retval & " occurances of the pattern: " &  " in the file: " & $filename)
; Serach stores with inventory set to all
$StrIntSetALL = "<AREA data_type=" & chr(34) & "49" & chr(34) & " option=" & chr(34) & "2" & chr(34) & " include_blobs="
EndIf
 
 
$msg = FileRead($filename, 1000)
EndFunc

Edited by Tony27

[spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler]

Never compromise even in the face of Armageddon

 

 

[/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler]

Link to comment
Share on other sites

  • Moderators

FYI, there are xml udf's in the forum if you do a search for them.

Your regex doesn't appear to be correct.  You took out the forcing it to do each line (?m), and you want it to be case insensitive I'm sure (?i), which is why I had (?im) at the beginning of the search string.

From your post, I don't know if you're telling me it's working, it's not working, or what your issue may be if it's not working.

Without actual string data, I can only guess, and I don't wish to do that.  Make a mock file if you need to, but provide the means for us to help you help yourself ;) .

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

SmOke_N,

You are the man, the last thing I want is to make you mad. :)

My brain feels like scramble eggs, guess it the result of not sleeping and not eating right. Anyhow, the code is not working as it should. I want the script to tell the user, what it found before "sCUSTOMER.XML "  or the whole string in the line. So I was thinking since what I am searching will be unique in the file I could just search for that, get the line that the searched string is in it and then display the whole line.

Here is what my file will look like, these are just a couple of lines. The original file has thousands and thousands lines. 


 
1. d         sysa             File Size 204 bytes. CPS: 0.                                                                                                                                                                                                                                   2. ÞÞ
3.
4. d         sysa    
5.ddddddd     
6.C:ECMPOLLINGs001101AINPARTIALsCUSTOMER.XML                                                                                                                                                                                                                 7.ÞÞ
 

 
So, I will be looking for "sCUSTOMER.XML " and ask my script to tell the user what it see before it, or get the line which in this case is "6"  put in a variable and run another function to display line "6" 
 
I know I am missing something really stupid in here, and I appreciate the time you spent with this little problem of mine. 
Edited by Rorschach

[spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler]

Never compromise even in the face of Armageddon

 

 

[/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler]

Link to comment
Share on other sites

  • Moderators

Something like this perhaps?

; for _arraydisplay func
#include <Array.au3>

Global $gsFileStr = ""
$gsFileStr &= "1. d         sysa             File Size 204 bytes. CPS: 0.   " & @CRLF
$gsFileStr &= "                                                  2. ÞÞ" & @CRLF
$gsFileStr &= "3." & @CRLF
$gsFileStr &= "4. d         sysa    " & @CRLF
$gsFileStr &= "5.ddddddd   " & @CRLF
$gsFileStr &= "6.C:\ECM\POLLINGs\001101A\IN\PARTIAL\sCUSTOMER.XML    " & @CRLF
$gsFileStr &= "                                                  7.ÞÞ"

Global $gaMatches = _myGetDataRegExFunc($gsFileStr, "sCUSTOMER.XML")
_ArrayDisplay($gaMatches)

Func _myGetDataRegExFunc($sFileStr, $sFind, $fStrict = True)

    ; if the file exists, read the file, if not, assume a string was passed with the data
    $sFileStr = (FileExists($sFileStr)) ? FileRead($sFileStr) : $sFileStr

    Local $sPattern = "\Q" & $sFind & "\E"
    $sPattern = ($fStrict) ? "\b" & $sPattern & "\b" : $sPattern
    $sPattern = "(?im)^\s*(.*?" & $sPattern & ".*?)\s*$" ; removing leading and trailing spaces, remove \s* if you don't want to

    ; uncomment consolewrite below to see what it looks like
;~  ConsoleWrite($sPattern & @CRLF)

    Local $aFound = StringRegExp($sFileStr, $sPattern, 3)
    If @error Then
        Return SetError(1, 0, 0)
    EndIf

    ; this is going to return an array with all the matches
    Return $aFound
EndFunc

Now I didn't know if you put those numbers there, if you didn't, it would probably be a neat idea to put it in a 2D array so you know programatically what lines those were found on:

In this example:

[n][0] = the line number it was found on.

[n][1] = the value found on that line without the line number in it

; for _arraydisplay func
#include <Array.au3>

Global $gsFileStr = ""
$gsFileStr &= "1. d         sysa             File Size 204 bytes. CPS: 0.   " & @CRLF
$gsFileStr &= "                                                  2. ÞÞ" & @CRLF
$gsFileStr &= "3." & @CRLF
$gsFileStr &= "4. d         sysa    " & @CRLF
$gsFileStr &= "5.ddddddd   " & @CRLF
$gsFileStr &= "6.C:\ECM\POLLINGs\001101A\IN\PARTIAL\sCUSTOMER.XML    " & @CRLF
$gsFileStr &= "                                                  7.ÞÞ"

Global $gaMatches = _myGetDataRegExFunc2D($gsFileStr, "sCUSTOMER.XML")
_ArrayDisplay($gaMatches)

Func _myGetDataRegExFunc2D($sFileStr, $sFind, $fStrict = True)

    ; if the file exists, read the file, if not, assume a string was passed with the data
    $sFileStr = (FileExists($sFileStr)) ? FileRead($sFileStr) : $sFileStr

    Local $sPattern = "\Q" & $sFind & "\E"
    $sPattern = ($fStrict) ? "\b" & $sPattern & "\b" : $sPattern
    $sPattern = "(?im)^\s*(\d*)\.*\s*(.*?" & $sPattern & ".*?)\s*$" ; removing leading and trailing spaces, remove \s* if you don't want to

    ; uncomment consolewrite below to see what it looks like
;~  ConsoleWrite($sPattern & @CRLF)

    Local $aFound = StringRegExp($sFileStr, $sPattern, 3)
    If @error Then
        Return SetError(1, 0, 0)
    EndIf
    
    ; set up 2D Array
    Local $aRet[UBound($aFound)][2], $iRDim = 0
    For $i = 0 To UBound($aFound) - 1 Step 2
        $aRet[$i][0] = $aFound[$i]
        $aRet[$i][1] = $aFound[$i + 1]
        $iRDim += 1
    Next

    ReDim $aRet[$iRDim][2]
    ; this is going to return a 2D array with all the matches and line numbers
    Return $aRet
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

This genius!!! I still need to iron out some stuff but I think I can finish from here. 

Thank you very much

EDITED:

Hey guys,

I know it is been a long time but I just wanted to post my solution. That way if someone stumble into the same problem they can get an idea. 

Even though Smoke_N was essential for me to get this right I ended up doing something different that he did. Hope he doesn't mind. 


Func StoreNumber($store)


$file = FileOpen("c:test.xml", 0)
while 1
$line = FileReadLine($file)
$global = "test"

$iPosition = StringInStr($line, $global )
$station = StringMid ( $line, $iPosition + StringLen($global)+4, 3)


if $iPosition <> 0 then

;MsgBox(0, "test", $line)
;MsgBox(0, "test", $iPosition)
;MsgBox(0, "test", $station)

Return $station





EndIf


WEnd
FileClose($file)
EndFunc

 

Again guys this is just my two cents

Edited by Rorschach

[spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler][spoiler]

Never compromise even in the face of Armageddon

 

 

[/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler][/spoiler]

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