Jump to content
Sign in to follow this  
jrmm

Search for string in text file.

Recommended Posts

jrmm

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
kylomas

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
jdelaney

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
InunoTaishou

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
jrmm

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
jdelaney

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
jrmm

@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  

  • Similar Content

    • reida
      By reida
      Hello. On page there is mail address. How to keep in a text file this address? 
      It is page and email
      https://alpari.com/
      info@alpari.com
    • nooneclose
      By nooneclose
      My program has to first search for names in Column D that do not match up with column C. I got that search to work using arrays. It was slow and I could not figure out how to delete them so I just manually put coded the names that do not belong. I found their cell location but I do not know how to store that location and delete it.
      This is what I have so far.
      Local $NameToDelete1[6]  = _Excel_RangeFind($OpenWorkbook, "Smith, Bill") _ArrayDisplay($NameToDelete1, "Excel UDF: _Excel_RangeFind Example 1", "", 0, "|", "Sheet|Name|Cell|Value|Formula|Comment") _Excel_RangeDelete($OpenWorkbook.ActiveSheet, $NameToDelete1[2], $xlShiftUp)  
      Please help, I wanted to have this program done yesterday but I did not see this problem until yesterday. 
    • caramen
      By caramen
      Hey.  
       
      I requested help about how to get a value from a text in a variable. 
      Now i know how to do that. But i learned with the command FileRead. Now i whould like to know how to replace the command :
      FileRead('Dossier.txt') The purpose is to read a webpage text. To find some value inside. 
       
      Btw i tryed to play with WindowsInfo.au3 but i dont got much thing.  
    • AndyS19
      By AndyS19
      I'm trying to implement a Ctl-F popup box that looks something like the one that Notepad uses, but I'm not havine much luck.  I intend to get it working, then beef up the popup's contents to add several checkboxes, buttons and radio boxes.
      What my example code does is to use InputBox(), but that's not what I want.
      Here is my test code:
      #include <Array.au3> #include <GUIConstantsEx.au3> #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 Opt("GUICloseOnESC", 1) Opt("GUIOnEventMode", 1) Opt('MustDeclareVars', 1) OnAutoItExitRegister("ExitStageLeft") Opt("WinTitleMatchMode", -2) Global $hGUI _Main() Func _Main() $hGUI = GUICreate("Test ^F", 300, 200) setupSpecialKeysHandlers() GUISetOnEvent($GUI_EVENT_CLOSE, "Event_GUIClose") GUISetState() While (1) Sleep(157) WEnd EndFunc ;==>_Main Func handle_CTRL_F_key() Local $str $str = InputBox("Search", "Enter the string to search for:") ConsoleWrite("+++: $str ==>" & $str & "<==" & @CRLF) EndFunc ;==>handle_CTRL_F_key Func ExitStageLeft() Exit (99) EndFunc ;==>ExitStageLeft Func Event_GUIClose() Exit (1) EndFunc ;==>Event_GUIClose Func setupSpecialKeysHandlers() Local $ar, $parts, $key, $handler, $id Local $aAccelKeys[1][2] ; Create a table of Special keys and their handlers $ar = StringSplit("", "") _ArrayAdd($ar, "^f - handle_CTRL_F_key ") ReDim $aAccelKeys[UBound($ar) - 1][2] ; Now, create $aAccelKeys array with the table data. ; For each entry, create a Dummy GUI and associate its ; ID with the special key. For $ndx = 1 To UBound($ar) - 1 $parts = StringSplit($ar[$ndx], "-", 2) $key = StringStripWS($parts[0], 8) $handler = StringStripWS($parts[1], 8) $id = GUICtrlCreateDummy() $aAccelKeys[$ndx - 1][0] = $key $aAccelKeys[$ndx - 1][1] = $id GUICtrlSetOnEvent($id, $handler) Next GUISetAccelerators($aAccelKeys) ; Setup the Special keys hooks EndFunc ;==>setupSpecialKeysHandlers  
    • Atoxis
      By Atoxis
      Howdy, I've gone through a lot of au3 forums, and I once had a working Imagesearch script that I got from here.  However, and i'm just totally not sure how but my imagesearch scripts aren't working anymore.
      I'm not new to au3 but i'm not the most experienced with it's syntax/commands.

      Anyways, I've looked over the big threads involving imagesearch.

      Does anyone have a working Imagesearch x64 for win10 that is currently working as of the date with the post.

      Dll's and what not is fine, just when I tell the script to run, I want to be able to find the image on the screen!
      Can't find a working copy so if anyone has one please send it my way lol.

      I've taken all the imagesearch downloads and what not and have played with them but I can't get any of them working on my end, despite others saying they're working.
      Thanks.
×