Jump to content

[Solved] Find string in file, find line, get string from this line


HeJake
 Share

Recommended Posts

Hello.

I have a question with subject.

Through searching forum i found this code.

_FileReadToArray($File, $aRecords)

For $x = 1 to $aRecords[0]

If StringRegExp($aRecords[$x], $String) Then

$Line = FileReadLine($File2, $x)

$FA = StringLeft($Line,3)

GUICtrlSetData($Label, $FA)

EndIf

Next

2 files, one contains strings and the second, numbers. So, this will set label to number on selected line by finding string in 1st file.

But there is one problem. If the file contains for example: Haagrid and then Haagrid Blefo, script will "choose" Haagrid Blefo because of next. So there is a question, any way to make it stop when found 1 match? Or even better, to search only "whole" string? E.g: Haagrid, and no Haagrid Blablabla or Blablabla Haagrid or Blablabla Haagrid Blablabla. ( Not only Haagrid :), just in case of string length)

Edited by HeJake
Link to comment
Share on other sites

Yes,

_FileReadToArray($File, $aRecords)

For $x = 1 to $aRecords[0]

If StringRegExp($aRecords[$x], $String) Then

$Line = FileReadLine($File2, $x)

$FA = StringLeft($Line,3)

GUICtrlSetData($Label, $FA)

ExitLoop

EndIf

Next

BUT, as i said about Blablabla, if BlaBlaBla Haagrid on the first line, and then Haagrid on the second, the first one will be choosen as first match, but i need the second, because i search for Haagrid, not for Blablabla Haagrid :)

I just cant figure out how to make it :)

Link to comment
Share on other sites

  • Moderators

HeJake,

Using this file:

Haagrid Blablabla
Blablabla Haagrid
Haagrid
Blablabla Haagrid Blablabla

this script will pull out the single "Haagrid":

#include <File.au3>

Global $aLines
_FileReadToArray("List.txt", $aLines)

$sSearch = "Haagrid"

For $i = 1 To $aLines[0]
    ; Search for the exact match
    If $aLines[$i] == $sSearch Then
        MsgBox(0, "Found", $sSearch & " found on line " & $i)
        ; No point in searching further
        ExitLoop
    EndIf
Next

Please ask if you have any questions. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Yes, thanks, it works if the line has a letters with no other characters. However, i need to find Haagrid in file like

( Tabs between numeric and string )

101 Haagrid Blablabla
102 Blablabla Haagrid
103 Haagrid
104 Blablabla Haagrid Blablabla

And then retrieve 103 ( in our case ), that i was doing with

$FA = StringLeft($i,3)
Edited by HeJake
Link to comment
Share on other sites

  • Moderators

HeJake,

Top Tip: Give as much information as you can when you ask a question - then we do not waste time working on code that does not do what you want. We cannot read your mind to determine what your StringLeft line is supposed to do. ;)

You will need to use a RegEx to make sure that there is no other text on the line:

#include <File.au3>

Global $aLines
_FileReadToArray("List.txt", $aLines)

$sSearch = "Haagrid"

For $i = 1 To $aLines[0]
    ; Search for the exact match
    If StringRegExp($aLines[$i], "d{3}s" & $sSearch & "z") Then
        MsgBox(0, "Found", $sSearch & " found on line " & $i & @CRLF & @CRLF & "Code = " & StringLeft($aLines[$i], 3))
        ; No point in searching further
        ExitLoop
    EndIf
Next

The SRE looks for 3 digits followed by a space, the required text and then the end of the string. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

HeJake,

Another change in the goalposts! You are rapidly using up my goodwill quota for today! :P

If you always have a random number of digits ahead of the space and the text, you could try this:

#include <File.au3>

Global $aLines
_FileReadToArray("List.txt", $aLines)

$sSearch = "Haagrid"

For $i = 1 To $aLines[0]
    ; Search for the exact match
    If StringRegExp($aLines[$i], "Ad*s" & $sSearch & "z") Then
        MsgBox(0, "Found", $sSearch & " found on line " & $i & @CRLF & @CRLF & "Code = " & StringRegExpReplace($aLines[$i], "(d*)s.*", "$1"))
        ; No point in searching further
        ExitLoop
    EndIf
Next

But how about posting an example of the real data you are using so we can get a better pattern and not proceeed in tiny steps? ;)

M23

P.S. Decode:

SRE

d*      - Any number of digits
s       - a space
$sSearch - the search term
z       - the end of the string

SRER

(d*)    - Capture any number of digits followed by..
s.*     - a space and any number of characters

$1       - Replace it all by just the digits

Al clear? :)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you! and Sorry :> You answered on my another question :D , about "how to" if number of digits can change.

Now its okay. I was anyway on the begin.

You still want to see my code?

Btw, no option to change thread name ( HELP to Solved, or at least to delete HELP )

Link to comment
Share on other sites

  • Moderators

HeJake,

No need to post the data if what I posted works. :)

To change the title, edit the first post using the "Use Full Editor" option - that opens up the title for editing as well. Now you have 5 posts you should see the "Edit" button at bottom right of your posts. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Sorry to disturb again.

Through listing strings, found ( unexpected ) that some of them can be like Numbers{tab}Word\s ( Numbers\letters ).

So, i tried to add something like:

#include

Global $aLines
_FileReadToArray("List.txt", $aLines)

$sSearch = "TW BOY (4 M)"

For $i = 1 To $aLines[0]
; Search for the exact match
If StringRegExp($aLines[$i], "\A\d*\s\" & $sSearch & "\(\?.\)" & "\z") Then
MsgBox(0, "Found", $sSearch & " found on line " & $i & @CRLF & @CRLF & "Code = " & StringRegExpReplace($aLines[$i], "(\d*)\s.*", "$1"))
; No point in searching further
ExitLoop
EndIf
Next

Or i am sure its wrong :)

Here is part of the file:

10772 TW MASKGIRL
10773 TW MIDWOMAN (4 F)
10774 TW BOY (4 M)
10775 TW MASKMAN (4 M)
10776 TW MIDMAN (4 M)
10777 TW TEAMAN (4 M)
Link to comment
Share on other sites

  • Moderators

HeJake,

Why use a RegEx at all on that list? There is no repetition (like there was with all the Haagrids earlier) and a simple StringInStr should do the trick. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

The whole thing keeps changing.

Anyway, here's my interpretation of what's requested.

; would be the search file name, but just going to use contents for testing
Global $gs_TempFile = "10772    TW    MASKGIRL" & @CRLF
$gs_TempFile &= "10773    TW    MIDWOMAN    (4 F)" & @CRLF
$gs_TempFile &= "10774    TW    BOY    (4 M)" & @CRLF
$gs_TempFile &= "10775    TW    MASKMAN    (4 M)" & @CRLF
$gs_TempFile &= "10776    TW    MIDMAN    (4 M)" & @CRLF
$gs_TempFile &= "10777    TW    TEAMAN    (4 M)"

ConsoleWrite("Exact Match; Case Insensitive -> " & _my_filesearch_getdigit($gs_TempFile, "TW    BOY    (4 M)") & @CRLF) ; should be boy 10774
ConsoleWrite("Exact Match; Case Sensitive   -> " & _my_filesearch_getdigit($gs_TempFile, "TW    bOY    (4 M)", True, True) & @CRLF) ; should be @error and 0 for no match
ConsoleWrite("Match Any; Case Insensitive   -> " & _my_filesearch_getdigit($gs_TempFile, "TW    Mask", False) & @CRLF) ; should be maskgirl 10772
ConsoleWrite("Match Any; Case Sensitive  -> " & _my_filesearch_getdigit($gs_TempFile, "TW    MASKM", False, True) & @CRLF) ; should be maskman 10775

Func _my_filesearch_getdigit($s_file, $s_find, $f_exact = True, $f_casesensitive = false)

    ; assume string (test maybe) was passed
    Local $s_fread = $s_file
    ; see if string passed is a file
    If FileExists($s_file) Then $s_fread = FileRead($s_file)
    ; see if there is length to, typically file doesn't exist
    If Not StringLen($s_fread) Then Return SetError(1, 0, 0)

    ; case insensitive pattern match
    Local $s_pattern = "(?mi)"
    If $f_casesensitive Then $s_pattern = "(?m)"

    If $f_exact Then
        ; return exact match
        $s_pattern &= "^(d+)tQ" & $s_find & "E(?:z|v)"
    Else
        ; return first match
        $s_pattern &= "^(d+)t(?:.*?Q" & $s_find & "E)"
    EndIf

    ; look for pattern
    Local $a_find = StringRegExp($s_fread, $s_pattern, 1)
    ; match found? return data
    If Not @error Then Return $a_find[0]

    ; no match
    Return SetError(2, 0, 0)
EndFunc

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

Thank you both for your help!

Just changed this

If StringRegExp($aLines[$i], "\A\d*\s" & $sSearch & "\z") Then

To:

If StringRegExp($aLines[$i], "\A\d*\s\Q" & $sSearch & "\E(?:\z|\v)") Then

And its work :) Both for [] and ().

HeJake,

Why use a RegEx at all on that list? There is no repetition (like there was with all the Haagrids earlier) and a simple StringInStr should do the trick. ;)

M23

When i used StringInStr, its was incorrect, for example:

1000 Boy Mask
1001 Mask

So if i need to search Boy Mask, was the first line, and if i need to search Mask, it was again the first line :D

So i choose StringRegExp, but didn't knew how to use flags.

Link to comment
Share on other sites

  • Moderators

HeJake,

I am glad you solved your problem, but permit me to offer some advice for your future postings. ;)

All throughout this thread you have constantly changed your requirements. This means that those who offer help are forever finding that the solutions they have offered are not really valid as they do not solve the real problem with which you are faced. Not only is this extremely annoying at the time, it also means that those who have wasted their time replying to you are much less likely to help you in future. So when you post again, please give as complete a set of information as possible in the first post so that we have a valid data set upon which to work.

All clear? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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