Help with StringSplit to read from second line Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Help with StringSplit to read from second line

24 Jul 2017, 04:39

im read some log file and want to show only a specific word from it.

here is the logfile:
  • SESSIONNAME USERNAME ID STATE TYPE DEVICE
    >services 0 Disc
    0123 18 Disc
    console 19 Conn
    rdp-tcp 65536 Listen


the specific word i want to show from the above log file is: 0123.
im using the word "disc" to find this specific word,
problem is i have "disc" twice in the log file, therefore it refers to the 1st "disc" and then i get "0",
if i delete manually the 1st "disc" from the log file, then its works (because now i have only 1 disc word).
i know i can use the number "18" instead "disc" to show the word i want, but "18" is a value that changed all time and "disc" is not.
so i thought how can i refer it to the 2nd "disc" to read the word i want ?

the code:

Code: Select all

log = c:\1.log

FileRead,log_file,%log%
log_filecsv =  % RegExReplace(log_file, a_space "{2,}","`,")
Loop,Parse, log_filecsv, `n
{
	line = %A_LoopField%
IfInString,line,disc
{
StringSplit,line,line,`,
break

}
}

MsgBox, %line2%
* its importnat for me this keep this "style" of code because i use it to read other lines.

Thanks in advance!
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Help with StringSplit to read from second line

24 Jul 2017, 05:03

Code: Select all

Loop, Read, my.log
   {
   If InStr(A_LoopReadLine, "Disc") && !InStr(A_LoopReadLine, ">services"){
      num := StrSplit(A_LoopReadLine,A_Space)
      MsgBox % num[1]
      }
   }
Last edited by BoBo on 24 Jul 2017, 05:26, edited 1 time in total.
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Help with StringSplit to read from second line

24 Jul 2017, 05:22

tnx BoBo
but this not working :(
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Help with StringSplit to read from second line

24 Jul 2017, 05:26

Tomer wrote:tnx BoBo
but this not working :(
Fixed.
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Help with StringSplit to read from second line

24 Jul 2017, 05:45

still not working

however,
i manged to solve it with replace disc with 1:

IfInString,line,1

i dont know why, but this solved it....
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Help with StringSplit to read from second line

24 Jul 2017, 05:56

Tomer wrote:still not working

however,
i manged to solve it with replace disc with 1:

IfInString,line,1

i dont know why, but this solved it....
I've tested my script snippet, and it works. Even with multiple occurrences of that data. You simply had to change the path to the input file. That's it.
Well, AlphaBravo is right (so, you too. Sort of). :wtf:
I've parsed against your sample input format (what makes sense bc the delimiter conversion you're doing in your code is (IMHO) an extra, but unnecessary step) :shh: :eh:

As AlphaBravo pointed out already: That would've worked for a comma-delimited input file: num := StrSplit(A_LoopReadLine,",")
Last edited by BoBo on 24 Jul 2017, 09:50, edited 3 times in total.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Help with StringSplit to read from second line  Topic is solved

24 Jul 2017, 09:29

Tomer wrote:

Code: Select all

log = c:\1.log

FileRead,log_file,%log%
log_filecsv =  % RegExReplace(log_file, a_space "{2,}","`,")
Loop,Parse, log_filecsv, `n
{
	line = %A_LoopField%
IfInString,line,disc
{
StringSplit,line,line,`,
break

}
}

MsgBox, %line2%
You're splitting using a comma, then looking at the value of line2!!, you should be looking at the value of line1 instead.
try this

Code: Select all

Loop,Parse, log_filecsv, `n
{
	line = %A_LoopField%
	IfInString,line,disc
	{
		StringSplit,line,line,`,
		if line1 is number
			break
	}
}
MsgBox, %line1%
BoBo wrote:num := StrSplit(A_LoopReadLine,A_Space)
you should split by commas as he changed spaces to commas prior to "loop, Parse"
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Help with StringSplit to read from second line

25 Jul 2017, 03:50

Thank you BoBo & AlphaBravo !

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, NinjoOnline and 278 guests