Jump to content

Search a document for a word on 2 different lines


 Share

Recommended Posts

Is there a better way to search a file for a particular word such as "Part" and then read the next line for "Numbers" and combine the 2 words to make a determination if the word combination for "Part Numbers" is 100%? I am able to find Part but I'm not sure how to make a recursive loop to see if "Numbers" is immediately below the "Part". 

ReadTxtFile()

Func ReadTxtFile()
    $sFileName = "DimpSheet.txt" ;Filename here
    $sWord = 'Part Numbers' ;Search the word here

    $sText = FileRead($sFileName, FileGetSize($sFileName))
    If StringInStr($sText, $sWord) Then
        MsgBox(0, '', "Word is found in file!")
    Else
        MsgBox(0, '', "Not found in file!")
    EndIf
EndFunc   ;==>_ReadTxtFile

 

Edited by aa2zz6
Link to comment
Share on other sites

  • Moderators

So, you search the doc line by line - on the line you find the word "Part", you check for the very next line to read "Numbers", am I getting that right? An example of the word document would be awesome so we can see what you're expecting to see.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

When I extract data from our map to a document the fields names will sometimes move down 1 line which is supposed to be the line for input data by our operators. I was wondering if there was a simpler way to detect errors and resolve them in a timely manor.

Map img.png

Fields.png

Parsed.txt

Link to comment
Share on other sites

It's still not clear from the text file. Anyway, you might want to use FileReadToArray() - see help file. Once you have read the file to an array, perhaps you can do something like the following. As you can see, I'm not entirely sure if you are looking for the word numbers or actual numbers, so I give two examples.

#include <Array.au3>

Local $aArray = ["part","numbers","part","6.7","party","numbers","rand","part","more numbers"]
_ArrayDisplay($aArray)

ConsoleWrite("First Example:"  & @CRLF)
For $i = 0 To UBound($aArray) -2 ; only loop as far as the penultimate element - to avoid array bounds errors
    If StringInStr($aArray[$i], "part") And StringInStr($aArray[$i +1], "numbers") Then
        ConsoleWrite("verified at elements " & $i & " and " & ($i +1) & " - " & $aArray[$i] & " " & $aArray[$i +1] & @CRLF)
    EndIf
Next

ConsoleWrite(@CRLF & "Second Example:"  & @CRLF)
For $i = 0 To UBound($aArray) -2
    If StringInStr($aArray[$i], "part") And (StringIsDigit($aArray[$i +1]) Or StringIsFloat($aArray[$i +1])) Then
        ConsoleWrite("verified at elements " & $i & " and " & ($i +1) & " - " & $aArray[$i] & " " & $aArray[$i +1] & @CRLF)
    EndIf
Next

Of course, you need to decide exactly what you need to verify and probably use stricter filtering: partial or exact match with, or without, whitespace etc... Hopefully the code will give you some clues.

SEE EDITS

Edited by czardas
Link to comment
Share on other sites

When we change out the $array= ["Part", "Number", "ect"] with a $array= FileReadToArray is there a reason why the FileReadToArray doesn't work with the for loop but displays the results in an _ArrayDisplay($aArray)?

 

#include <MsgBoxConstants.au3>
#include <Array.au3>

ReadTxtFile()

Func ReadTxtFile()
    ; Read the current script file into an array using the filepath.
    Local $aArray = FileReadToArray("DimpSheet.txt")
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file.
    Else
        ConsoleWrite("First Example:" & @CRLF)
        For $i = 0 To UBound($aArray) - 1 ; only loop as far as the penultimate element - to avoid array bounds errors
            If StringInStr($aArray[$i], "Part") And StringInStr($aArray[$i + 1], "numbers") Then
                ConsoleWrite("verified at elements " & $i & " and " & ($i + 1) & " - " & $aArray[$i] & " " & $aArray[$i + 1] & @CRLF)
            EndIf
        Next
    EndIf
    _ArrayDisplay($aArray)
EndFunc   ;==>ReadTxtFile

 

For Loop.png

_ArrayDisplay.png

Link to comment
Share on other sites

  • Moderators

The ArrayDisplay is because you have it in your script.

 

35 minutes ago, aa2zz6 said:

 

#include <MsgBoxConstants.au3>
#include <Array.au3>

ReadTxtFile()

Func ReadTxtFile()
    ; Read the current script file into an array using the filepath.
    Local $aArray = FileReadToArray("DimpSheet.txt")
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file.
    Else
        ConsoleWrite("First Example:" & @CRLF)
        For $i = 0 To UBound($aArray) - 1 ; only loop as far as the penultimate element - to avoid array bounds errors
            If StringInStr($aArray[$i], "Part") And StringInStr($aArray[$i + 1], "numbers") Then
                ConsoleWrite("verified at elements " & $i & " and " & ($i + 1) & " - " & $aArray[$i] & " " & $aArray[$i + 1] & @CRLF)
            EndIf
        Next
    EndIf
    _ArrayDisplay($aArray) <================================
EndFunc   ;==>ReadTxtFile

 

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

14 minutes ago, JLogan3o13 said:

The ArrayDisplay is because you have it in your script.

 

 

I know it's in the script :D I just don't understand how I can switch out $array= ["Part", "Number", "ect"] with $array= FileReadToArray and the For loop not work anymore but clearly see that _arrayDisplay($aArray) is showing everything in the file as apart of the array.

Link to comment
Share on other sites

  • Moderators

You need to be more clear; "not work anymore" doesn't help. Using what you have above for a text file, your snippet of code works as I would expect it:

#include <Array.au3>

Local $aArray = FileReadToArray("DimpSheet.txt")
    For $i = 0 To UBound($aArray) - 1
        If StringInStr($aArray[$i], "Part") And StringInStr($aArray[$i + 1], "numbers") Then
                ConsoleWrite("verified at elements " & $i & " and " & ($i + 1) & " - " & $aArray[$i] & " " & $aArray[$i + 1] & @CRLF)
        EndIf
    Next

returns

Quote

verified at elements 0 and 1 - part numbers
verified at elements 4 and 5 - party numbers
verified at elements 7 and 8 - part more numbers

So please explain better what you're expecting but not seeing.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Hey @aa2zz6, why did you change the number of loops? It should only loop until it reaches UBound($aArray) -2 in this case. The reason is that we test the next subsequent element on every run. The only reason your (modified) version has not crashed yet is because the final element does not contain the string "part". If it did, then the code would try and test to see if the last element +1 contains "numbers", and therefore crash because the array doesn't contain any more elements. The fact it hasn't crashed already is only because you got lucky.

EDIT: If you find this confusing, don't worry everyone does at first. Look at the _ArrayDisplay() image you posted above. Compare the row numbers with the number of elements. There are 9 elements, but notice that the rows are numbered from 0 to 8.

Edited by czardas
Link to comment
Share on other sites

19 hours ago, aa2zz6 said:

When I extract data from our map to a document

Assuming that the "Parsed.txt" provided in post #3 is the exact result (if not I'm totally wrong ^_^), this could be a funny way 

#Include <Array.au3>

Local $s = FileRead("Parsed.txt")
$s = StringRegExpReplace($s, '(?<=Nominal\h)\R(?=Diameter)', "")
$s = StringRegExpReplace($s, '(?<!\h)\R(?!\s*$)', "=")
$s = StringRegExpReplace($s, '(?m)^([^=\v]+)\h$', "$1=")
;Msgbox(0,"", $s)

Local $temp_ini = @tempdir & "\crazytest.ini"
FileWrite($temp_ini, "[test]" & @crlf & $s)
$a = IniReadSection($temp_ini, "test")
FileDelete($temp_ini)
_ArrayDisplay($a)


Edit  :)
BTW ,

19 hours ago, aa2zz6 said:

Is there a better way to search a file for a particular word such as "Part" and then read the next line for "Numbers" and combine the 2 words

For this, you can replace the EOL sequence (@crlf or so) between these two words by nothing

$string = "part one " & @crlf & "part two " & @crlf & "part " & @crlf & "three " & @crlf & "part four " 
Msgbox(0,"", $string)

$string = StringRegExpReplace($string, '(?<=part)\h*\R(?=three)', " ")
Msgbox(0,"", $string)

 

Edited by mikell
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...