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

    • 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
    • houser747
      By houser747
      I have previously used _IEFormElementGetObjByName and _IEFormElementSetValue to enter text into a search box on a form and then submit the form.
      I am now trying to enter text into a search box which is not part of a form. 
      Here is the HTML from the website that i'm trying to enter the data on and then submit the search.
      <div class="row">
          <div class="form-group col-xs-12">
              <span id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_lblSearchText" for="input-search">Registreringsbeteckning</span>
              <div class="input-group col-xs-12">
                  <span id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_preSearchText" class="input-group-addon">SE -</span>
                  <input name="ctl00$FullWidthWithSubmenuContent$FullWidthContent$MainContent$AircraftRegistry$txtSearchText" type="text" value="DTH" id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_txtSearchText" class="form-control" />
              </div>
          </div>
      </div>
      <div class="row">
          <div class="form-group col-xs-12">
              <label class="sr-only" for="">Sök</label>
              <input type="submit" name="ctl00$FullWidthWithSubmenuContent$FullWidthContent$MainContent$AircraftRegistry$btnSearch" value="Sök" id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_btnSearch" class="btn btn-primary ladda-button" data-style="expand-right" />
          </div>
      </div>
      Many thanks in advance
      cheers
      Roger
    • algiuxas
      By algiuxas
      Hello everybody, so I might found a bug in _ArraySearch:
       
      #include <Array.au3> Local $abc[2] = ["b", "b"] $h = "+---------------------------------+" & @CRLF ConsoleWrite($h) For $i = 0 To 1 $abc[0] = 0 out() $abc[0] = 1 out() $abc[0] = -1 out() $abc[0] = "Abc" out() $abc[1] = "Hello" Next Func txt() Return "$abc = [" & $abc[0] & ", " & $abc[1] & "]" & @CRLF & _ "> _ArraySearch($abc,""Hello"") = " EndFunc ;==>txt Func shouldoutput($ans) Return "_ArraySearch Should return: " & ($ans ? 1 : -1) EndFunc ;==>shouldoutput Func out() ConsoleWrite("> " & txt() & _ArraySearch($abc, "Hello") & @CRLF & "> " & shouldoutput($abc[1] == "Hello") & @CRLF & $h) EndFunc ;==>out Exit Sorry for this messy script
    • robcull
      By robcull
      Hello all! I have had some issues reading text from different types of windows, occasionally, specifically with controlgettext. 
      **Before I begin, I know there are better ways to do what I attempt in the example below. That's not the point of this post. The point is my issues with controlgettext. 
      I am about to cite an example with an application you may be familiar with called SpeedFan (v4.52). My problem is not specific to speedfan, it is simply the most recent and easily reproducible example I can think of. 
      So, the goal of the script below is to get a string of text containing the current fan RPMs from the highlighted control in the screenshot below (see "speedfan_control_details.png").

      Now, here's a simple script for grabbing the window handle and reading the text from that control: 
      $wintitle = "SpeedFan 4.52" $controlID = "197934" ;will be reformatted as "[ID:######]" $hwnd = wingethandle($wintitle) if @error<>0 then msgbox(0, "WinGetHandle", "FAILURE. @error="&@error) Exit EndIf $text = ControlGetText($hwnd, "", "[ID:"&$controlID&"]") if @error=1 then msgbox(0, "ControlGetText", "FAILURE. @error="&@error) ;failure returns "" and @error=1 Exit EndIf msgbox (0, "ControlGetText", "SUCCESS. @error="&@error &@CRLF& "$text="&$text) ;success returns string and @error=0 You'll see that the ControlGetText operation runs without error, however it does not capture any text from the control. If you explore the other controls in this one window, you'll find mixed results across the board. Neither the temps nor voltages can be read, while the log field and some other elements can be read. Even when you read the text from the whole window, those elements are not included in the visible nor hidden texts. 
       
      I have run into this issue many times in the past- inconsistencies in the ability of autoit to interact with certain controls. What is it which makes this text different than any other readable texts? Is there an alternate method of reading the text in the window/control which could work? Any and all info to help me solve this mystery and satisfy my curiosity would be greatly appreciated. 
      Thanks  -Rob C
      PS: Running Autoit v3.3.14.2 on Win7 Ultimate x64
    • wakillon
      By wakillon
      Mp3SearchEngine v2.0.0.6

      May be some of you know Songr .
      This script do the same job, it can find more mp3 files but is not as fast as Songr.
       
       



      Sites used are music search engine Websites designed for LEGAL entertainment purposes only.
      Thanks to Brett Francis, Prog@ndy and Eukalyptus for >Bass Udf, trancex for >WinHttp Udf and the AutoIt Community for his help.

       
      Changes of v1.0.8.5
       
      Three websites replaced cause they are dead or use now js.
      All search engines updated ( not without difficulties for audiodump)
      I use RAGrid.dll for the first listview (more fast and stable, but with some inconvenients to manage the no-edit of cells)
      Input queries are saved ( the twenty latest)
      I use now an mp3 pre-Load management before playing and a double progressbar for visualize pre-load and play, where you can click for directly go play in the loaded part.
      Most includes needed are embedded and all external files are embedded in script with >BinaryToAu3Kompressor .
      Multi downloads available with embedded downloader.exe
       
      Changes of v1.0.8.8
      Search on audiodump and myfreemp3 fixed.
      New buttons.
      Added Gui Menu.
      Titles are no more editable.
      New "About" with >TaskDialog (Thanks Prog@andy)
      Query button permit now to check / uncheck all checkboxes
      And some few fixes and cleaning.
      Really more stable now.
      Changes of v1.0.9.2
      Dilandau is replaced by mp3chief and mp3ili by mp3clan 
      Search on mp3juices, baseofmp3 and soundcloud fixed.
      Soso now provide m4a (aac) instead of mp3 ( m4a can be played by MSE)
      Added possibility to encode automaticaly to mp3, aac or ogg ( at the end of download) using bassenc.dll and command line tools : lame, faac and oggenc.
       
      Changes of v1.0.9.3   mp3skull fixed mp3chief fixed myfreemp3 fixed mp3clan changed to tusmp3  mp3juices changed to emp3world baseofmp3 changed to imp3 and some minor improvements.  
      Version 2.0.0.6
      Most previous websites used are dead or have changed the way to get links, 
      so instead of try to repair the previous version, i have created a complete new version.
      The main tendency is the simplification :
      Only one website : audiodump (Up to 500 results by request)
      Script use now the little pearl created by Ward : curl.au3
      It permit to create tasks (get source and get multi mp3) in asynchronous mode.
      So now, no need to use several executables and no more gui who do not respond in case of connection problems. 
      Script use Bass.dll X86 loaded in memory for play songs.
      Result is light and fast, but don't abuse of audiodump servers who are not beasts of race.
      Warning : For avoid errors with curl.au3, you'll need to comment the line 63 : ;~ #Include <BinaryCall.au3>
      @AutoItX64 not supported and only tested on Win7X64 and Win8.1X64.
      As your browser, use Ctrl+w for remove the current Tab.(if there is no search or download running from it)
      And also Ctrl+q for set/remove Gridlines.
      Events are displayed to the bottom of the Gui.
       
      Version 2.0.1.1
      Added a Paste Button.
      Querry list is now correctly saved.
      Querry Combo is now sorted in alphabetical order
      After a 'No match', the next search will use the previous empty listview.
      Bug when removing tabs is corrected.
      Added string correction for the request that, in the previous version, was not always able to return a correct result.
       
      A big thanks to Ward for his great UDF, and Nina my favorite tester, (who between us is also my third daughter), for his precious advices .
      previous downloads : 1703
       
      As there is no more script downloads count, source and executable are available in the downloads section

      Enjoy ! 
      July 2017 Project Discontinued due to website changes