Search text in a file and display result

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gildamac
Posts: 9
Joined: 10 Mar 2017, 12:31

Search text in a file and display result

14 Mar 2017, 10:30

hello,

I need some help.

I would like to search a string in a file (always the same file) and then display this string and the rest of concerned line.

- Ex : the file name is : laison.txt
a little extract of the text file (very big text file 3mb)

101C COMMANDE ELECTRO COA ARG****
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE ARRIERE GAUCHE

101D COMMANDE ELECTRO COA AVD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE AVANT DROIT

101E COMMANDE ELECTRO COA ARD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE ARRIERE DROIT

- Entering the string to search in a textbox
- Ex : string to search "101D"


- And so display (in a msgbox or a tooltips) :
101D COMMANDE ELECTRO COA AVD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE AVANT DROIT

How to realize that simply (or not)
Thank you very much for your help. :)
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Search text in a file and display result

14 Mar 2017, 11:45

Hi Gildamac!
Il est en français ton fichier!

Code: Select all

string =
(
101C COMMANDE ELECTRO COA ARG****
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE ARRIERE GAUCHE

101D COMMANDE ELECTRO COA AVD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE AVANT DROIT

101E COMMANDE ELECTRO COA ARD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE ARRIERE DROIT 
)
A := StrSplit(string, "`n`n") ; we split the string (`n`n = empty line)

SearchForCommandId(__ID) {

global A ; to retrieve A defined without the function since all variable are implicitly local within a function

	Loop % A.MaxIndex() ; loops throught the splited string
	{
		if (SubStr(A[a_index], 1, 4) == __ID) { ; if the string starts with the ID
		return A[a_index] ; returns the ID
		}
	}
	return "La commande " . __ID . "n'a pas pu être trouvée." ; otherwise returns a message

} ; a function to search a command by its ID
MsgBox % SearchForCommandId("101D")
MsgBox % SearchForCommandId("101E")
MsgBox % SearchForCommandId("107D")
Hope it helps you :)
my scripts
Rohwedder
Posts: 7719
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Search text in a file and display result

14 Mar 2017, 12:56

Hallo,
this script search in C:\laison.txt

Code: Select all

InputBox, SearchString, Search in laison.txt, Entering the string to search
Line := False
Loop, Read, C:\laison.txt
{
	If InStr(A_LoopReadLine, SearchString)
		If !Line
		{
			Line := A_LoopReadLine
			Continue ;second concerned line
		}
		Else
		{
			Line .= "`r`n" . A_LoopReadLine
			Break
		}
}
MsgBox, % Line
gildamac
Posts: 9
Joined: 10 Mar 2017, 12:31

Re: Search text in a file and display result

14 Mar 2017, 13:01

A_AhkUser wrote:Hi Gildamac!
Il est en français ton fichier!

Code: Select all

string =
(
101C COMMANDE ELECTRO COA ARG****
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE ARRIERE GAUCHE

101D COMMANDE ELECTRO COA AVD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE AVANT DROIT

101E COMMANDE ELECTRO COA ARD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE ARRIERE DROIT 
)
A := StrSplit(string, "`n`n") ; we split the string (`n`n = empty line)

Hope it helps you :)
Hello A_AhkUser,

thank you very much for your french answer :bravo:

You solution work fine, but...
the file where to find the string is rather big (about 3Mo)...does the string commande (string =) will support this lenght ?
And if yes, how to convert the file in string ?

Thank you again
Rohwedder
Posts: 7719
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Search text in a file and display result

14 Mar 2017, 13:08

Hallo,
Please try my version above
gildamac
Posts: 9
Joined: 10 Mar 2017, 12:31

Re: Search text in a file and display result

14 Mar 2017, 13:33

Rohwedder wrote:Hallo,
Please try my version above
hello Rohwedder,

Your solution work fine :superhappy: for the first line, but don't write the second line.

In fact, if you search "101D"

the result must be

101D COMMANDE ELECTRO COA AVD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE AVANT DROIT

that is to say the line which contains 101D and the next line.
Rohwedder
Posts: 7719
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Search text in a file and display result

14 Mar 2017, 15:29

And now?

Code: Select all

InputBox, SearchString, Search in laison.txt, Entering the string to search
Line := False
Loop, Read, C:\laison.txt
{
	If !Trim(A_LoopReadLine)
		Continue
	If InStr(A_LoopReadLine, SearchString)
	{
		Line := A_LoopReadLine
		Continue ;second concerned line
	}
	If Line
	{
		Line .= "`r`n" . A_LoopReadLine
		Break
	}
}
MsgBox, % Line
gildamac
Posts: 9
Joined: 10 Mar 2017, 12:31

Re: Search text in a file and display result

14 Mar 2017, 15:35

Rohwedder wrote:And now?

Code: Select all

InputBox, SearchString, Search in laison.txt, Entering the string to search
Line := False
Loop, Read, C:\laison.txt
{
	If !Trim(A_LoopReadLine)
		Continue
	If InStr(A_LoopReadLine, SearchString)
	{
		Line := A_LoopReadLine
		Continue ;second concerned line
	}
	If Line
	{
		Line .= "`r`n" . A_LoopReadLine
		Break
	}
}
MsgBox, % Line

Perfect :bravo:

Thank you very much again.
marcxb
Posts: 6
Joined: 17 Aug 2016, 13:16

Re: Search text in a file and display result

05 Jul 2018, 09:48

Rohwedder wrote:And now?

Code: Select all

InputBox, SearchString, Search in laison.txt, Entering the string to search
Line := False
Loop, Read, C:\laison.txt
{
	If !Trim(A_LoopReadLine)
		Continue
	If InStr(A_LoopReadLine, SearchString)
	{
		Line := A_LoopReadLine
		Continue ;second concerned line
	}
	If Line
	{
		Line .= "`r`n" . A_LoopReadLine
		Break
	}
}
MsgBox, % Line
I could use that! Tell me is it possible to search for a string within a string...I mean, the line in the text file doesn't start with the searched string?

In fact, I have a .txt file with a bunch of file path in it. I would like to search with the filename within the filepath.

For instance:
I search "FileName1"

.txt file contain:
//Drive/FilePath/FileName0.txt
//Drive/FilePath/FileName0.pdf
//Drive/FilePath/FileName1.txt
//Drive/FilePath/FileName1.pdf
//Drive/FilePath/FileName2.txt
//Drive/FilePath/FileName2.pdf
//Drive/FilePath/FileName11.txt
//Drive/FilePath/FileName11.pdf

The script return (in a combobox or whatever):
FileName1.txt
FileName1.pdf
FileName11.txt
FileName11.pdf

The use can select a file to open...

But as the code is right now, it find line in the .txt file only if the line starts with the serched string.
Rohwedder
Posts: 7719
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Search text in a file and display result

05 Jul 2018, 11:34

Hallo,
try:

Code: Select all

InputBox, FullPath, Search File, Entering the Fullpath of File
InputBox, SearchString, Search in %FullPath%, Entering the String to search
Lines =
Loop, Read, %FullPath%
	If InStr(A_LoopReadLine, SearchString)
		Lines .= SubStr(A_LoopReadLine, 1+InStr(A_LoopReadLine,"/",,0)) "`r`n"
MsgBox, % Lines
marcxb
Posts: 6
Joined: 17 Aug 2016, 13:16

Re: Search text in a file and display result

06 Jul 2018, 06:47

Rohwedder wrote:Hallo,
try:

Code: Select all

InputBox, FullPath, Search File, Entering the Fullpath of File
InputBox, SearchString, Search in %FullPath%, Entering the String to search
Lines =
Loop, Read, %FullPath%
	If InStr(A_LoopReadLine, SearchString)
		Lines .= SubStr(A_LoopReadLine, 1+InStr(A_LoopReadLine,"/",,0)) "`r`n"
MsgBox, % Lines
That's it! Thank you very much! It works perfectly!
marcxb
Posts: 6
Joined: 17 Aug 2016, 13:16

Re: Search text in a file and display result

06 Jul 2018, 06:47

Rohwedder wrote:Hallo,
try:

Code: Select all

InputBox, FullPath, Search File, Entering the Fullpath of File
InputBox, SearchString, Search in %FullPath%, Entering the String to search
Lines =
Loop, Read, %FullPath%
	If InStr(A_LoopReadLine, SearchString)
		Lines .= SubStr(A_LoopReadLine, 1+InStr(A_LoopReadLine,"/",,0)) "`r`n"
MsgBox, % Lines
That's it! Thank you very much! It works perfectly!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 191 guests