Sign in to follow this  
Followers 0
jrmm

Search for string in text file.

7 posts in this topic

I love Autoit, but struggling to find the best way to do this.

I have 2 text files....  File1 is Tab Separated and File2 is CSV.  I am attempting to read in a line from File1 and split it, then search the contents of File2 for one of the substrings from that split line.    If I find a match, then I want to split the matching line and grab needed info from it to complete the record and output the combined record to a file.

 

Example line from File1:

    17   123456789     learned          10800     bridging      1/13

Example line from File2:

172.25.17.103,computername.domain,Reservation (active),DHCP,123456789,,Full Access,N/A

From File1, I grab the 2nd column (MAC Address) and search for that in the 2nd file (Column5)

 

I really thought reading file into an array and doing an _arraysearch was the way to go, but that seems to require searching for the whole string, not a sub-string.

What I have been trying to do so far is read in file1 one line at a time, split the string, then read in file 2 one line at a time and check for a matching substring..... handle that.   Then back to file 1 to repeat the process.   This seems so clunky.

So I would love suggestions on what functions might work best.

Thanks so much for any feedback.

- John

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Jrmm,

Can you post examples of the files...20 or so linesof each.  How large are the files on average?

Kylomas

Edit: is the search argument always the second delimited value from file one?

Edited by kylomas
question

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Here is a small example:

#include <Array.au3>
Local $aFile1
Local $aFile1
; this will be populated from _FileReadToArray("yourfile.txt",$aFile1,0)...i'm just populating an array since I don't have the file
Local $aFile1[3] = ["17 123456789   learned     10800   bridging    1/13","18   123456780   learned     10800   bridging    1/13","18   123456783   learned     10800   bridging    1/13"]

; this will be populated from _FileReadToArray("yoursecondfile.txt",$aFile1,0)...i'm just populating an array since I don't have the file
Local $aFile2[2] = ["172.25.17.103,computername.domain,Reservation (active),DHCP,123456789,,Full Access,N/A","172.25.17.104,computername.domain,Reservation (active),DHCP,123456780,,Full Access,N/A"]

; split up the array
For $i = 0 To UBound($aFile1)-1
    $aFile1[$i] = StringSplit($aFile1[$i],@TAB,2)
Next

; split up the array
For $i = 0 To UBound($aFile2)-1
    $aFile2[$i] = StringSplit($aFile2[$i],",",2)
Next

; find matches
For $i = 0 To UBound($aFile1)-1
    $aTemp = $aFile1[$i]
    For $j = 0 To UBound($aFile2)-1
        $aTemp2 = $aFile2[$j]
        If $aTemp[1] = $aTemp2[4] Then
            ConsoleWrite("Match for $sID=[" & $aTemp[1] & "] found in second file...=[" & _ArrayToString($aTemp2) & "]" & @CRLF)
            ExitLoop
        EndIf
    Next
    If $j=UBound($aFile2) Then
        ConsoleWrite("NO Match for $sID=[" & $aTemp[1] & "] found in second file..." & @CRLF)
    EndIf
Next

 

output:

Match for $sID=[123456789] found in second file...=[172.25.17.103|computername.domain|Reservation (active)|DHCP|123456789||Full Access|N/A]
Match for $sID=[123456780] found in second file...=[172.25.17.104|computername.domain|Reservation (active)|DHCP|123456780||Full Access|N/A]
NO Match for $sID=[123456783] found in second file...

As expected, the third item returns nothing, because it wsa not included in the second 'file'

You could also go a regexp route, which would be simpler....although less reliable, since I'm doing broad assumptions on a very simple regexp.

Local $aFile1
Local $aFile1
; this will be populated from _FileReadToArray("yourfile.txt",$aFile1,0)...i'm just populating an array since I don't have the file
Local $aFile1[3] = ["17 123456789   learned     10800   bridging    1/13","18   123456780   learned     10800   bridging    1/13","18   123456783   learned     10800   bridging    1/13"]

; this will be populated from FileRead("yoursecondfile.txt")...
Local $aFile2="172.25.17.103,computername.domain,Reservation (active),DHCP,123456789,,Full Access,N/A" & @CRLF &  _
"172.25.17.104,computername.domain,Reservation (active),DHCP,123456780,,Full Access,N/A"

; split up the array
For $i = 0 To UBound($aFile1)-1
    $aFile1[$i] = StringSplit($aFile1[$i],@TAB,2)
Next

; find matches
For $i = 0 To UBound($aFile1)-1
    $aTemp = $aFile1[$i]
    $aFound = StringRegExp($aFile2,"(.*" & $aTemp[1] & ".*)",1)
    If IsArray($aFound) Then
        For $j = 0 to UBound($aFound)-1
            ConsoleWrite("Match for $sID=[" & $aTemp[1] & "] found in second file...=[" & $aFound[$j] & "]" & @CRLF)
        Next
    Else
        ConsoleWrite("NO Match for $sID=[" & $aTemp[1] & "] found in second file..." & @CRLF)
    EndIf
Next

 

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Here was an interesting topic that was discussed for a few days about StringInStr with files. The original poster was trying to get it to work as fast as possible but maybe you could find some enlightenment in the topic.

 

Share this post


Link to post
Share on other sites

Thanks so much for the suggestions.   I am very grateful for the help with this.  

 

@jdelaney, I like your first example and think it will do the trick.    You do things in the code I have never done, so I will have to dig into this tonight or over the weekend.  

Running the first example as is, I get this error...  

 (25) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

If $aTemp[1] = $aTemp2[4] Then
If ^ ERROR

Pointing the script to  the files I get the same error.....    

I'll have to wrap my heard around this better after work.

- John

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

The script was assuming that every line in each file is 'valid', meaning that it  can be broken down into at least a certain amount of values in the stringsplit.

Since that appears to not be the case, you need to check that the bounds of the temp array are valid; if not do a continueloop.

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

@jdelaney
Thanks again....  I found the issue.  The first file wasn't really tab delimited as I thought... It was using a bunch of spaces instead of tabs.  

I modified the section where the first split happens to look like below and now it rolling along smoothly....  

; split up the array
For $i = 0 To UBound($aFile1)-1
    $aFile1[$i] = StringReplace($aFile1[$i],":","")  ;Takes colons out of MAC address to match format of other file.
    do
    $aFile1[$i] = StringReplace($aFile1[$i],"  "," ") ;Replaces multiple spaces with a single space
    Local $iReplacements = @extended
    until $iReplacements = 0
    $aFile1[$i] = StringReplace($aFile1[$i]," ",",") ;Replaces single space with coma
    $aFile1[$i] = StringSplit($aFile1[$i],",",2)  ;Split CSV
 Next

The script is working well.  Thank you so much.  I definitely learned some new tricks and sure appreciate the help.

- John

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

  • Similar Content

    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Guys this is my gui and i need the numeric value typed in the highlighted  inputbox. if this value is greater than 1 i need to perform some operations
      \
       
      Here is the summary about that field which i got from wininfo tool

      i tried writing the code as shown below.. but the msgbox is popping up empty value..when the same code is pasted under some button action i am able to access the value present in the field..but when placed alone as written in the attached script i am unable to fetch it... 

      here i am using the valu present in the field

      can you guys tell me what's wrong??? or how can i get that value?
       
      My_GUI.au3

    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      in need the path/text present int above shown input box to be copied or retrieved into a variable .. how can i do that?
       
      Thanks
       
    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Friends i am new to autoit and i am stuck in middle of automation of flashing tool..
      my requirement is i have a window and i have to get the value present in that window (below is the picture)

      in the above window i need to get the value of the highlighted field.the value of this filed keeps on changing...ii need to get the current value present in the field..below are the details of the window and field..

       
      CAN it be done?
       
      thanks

    • Gowrisankar
      By Gowrisankar
      Hello everyone,
      I'm working on a task where, a PDF file is opened (in IE browser) when I click a link in a website.
      I have to read the first page of the PDF to find particular strings. Can you please share some ideas?
    • DeathChicken
      By DeathChicken
      If possible please add or edit the comments to explain how this works.
       
      ;includes functions from other things
      #include <GDIPlus.au3>
      #include <ScreenCapture.au3>
      ;hotkeys
      HotKeySet("{ESC}", _exit)
      HotKeySet("{F1}", _scan)
      ;global variables
      Global $win_title                    ;name of the window
      Global $area_x                        ;
      Global $area_y                        ;
      Global $area_w                       ;
      Global $area_h                        ;
      Global $cursor                         ;
      Global $rect_file                      ;
      Global $hbmpscreen              ;
      Global $i=  1043                      ;moves the rect
      Global $ii=    378                      ;moves the rect
      Global $x=1044                        ;inner rect offset
      Global $y=501                          ;inner rect offset
      Global $hbmprect                    ;image inside rect
      Global $hscreen                        ;image whole screen
                                         
      _GDIPlus_Startup()                    ;?
      ;display hotkeys on screen
      ToolTip("Press F1 to scan | Press ESC to Exit",0,0)
                                          ;infinite loop to keep prog running
      While 1
          Sleep(100)
      WEnd
      Func _scan()
      ;~ reads your screen area:
      _read()
      ;~ converts screen captured into bmp
      _convert()
      ;~ .  Loads converted bmp to be read
      _loadBMP()
      ;~ compares the bmp of your scanned screen to the actual screen that is being displayed
      _compare()
      EndFunc
                                          ;reads screen
      Func _read()
          $hscreen = _ScreenCapture_CaptureWnd("", WinGetHandle($win_title), $area_x, $area_y, $area_x + $area_w, $area_y + $area_h, $cursor)
      EndFunc
                                          ;converts screen into bmp
      Func _convert()
          _GDIPlus_BitmapCreateFromHBITMAP($hscreen)
      EndFunc
                                          ;loads converted screen
      Func _loadBMP()
      _GDIPlus_BitmapCreateFromFile($rect_file)
      EndFunc
                                          ;compares savedBMP to current screen
      Func _compare()
      ;If(                              below code                                     ) = (                   below code           ) Then
      If _GDIPlus_BitmapGetPixel($hbmpscreen, ($i + $x) - $area_x, ($ii + $y) - $area_y) = _GDIPlus_BitmapGetPixel($hbmprect, $x, $y) Then
      ;display message box titled found with a message of found
      MsgBox("","","found")
      ;if above is not correct then
      EndIf
      EndFunc
                                          ;exit func
      Func _exit()
          Exit
      EndFunc