Help with library: TF.ahk

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Tomer
Posts: 336
Joined: 21 Aug 2016, 05:11

Help with library: TF.ahk

21 Jan 2021, 06:56

tf.ahk

How to use \D toggle for EXACT match so the correct result will be 1 and not 3 ?
Thanks in advance!

Code: Select all

#Include tf.ahk

text=
(join`r`n
1
2
3
4
10.2.10.2
10.254.50.60
10.254.50.61
10.254.50.6
10.10.10.10
10.2.1.100
)

FileDelete, Test_TestFile.txt
FileAppend, % text, Test_TestFile.txt

WordCount:=TF_Count(TF("Test_TestFile.txt", "WordCount") "\D" ,"10.254.50.6")

MsgBox % WordCount

User avatar
mikeyww
Posts: 5342
Joined: 09 Sep 2014, 18:38

Re: Help with library: TF.ahk

21 Jan 2021, 07:09

Possibly something like:

Code: Select all

WordCount := TF_Count(TF("Test_TestFile.txt", "WordCount") "\D" ,"10.254.50.6") > 0
Also:

Code: Select all

find   = 10.254.50.6
found := Instr("`n" text, "`n" find) > 0
MsgBox, 64, Result for %find%, % found ? "True" : "False"
User avatar
Tomer
Posts: 336
Joined: 21 Aug 2016, 05:11

Re: Help with library: TF.ahk

21 Jan 2021, 07:17

mikeyww wrote:
21 Jan 2021, 07:09
Possibly something like:

Code: Select all

WordCount := TF_Count(TF("Test_TestFile.txt", "WordCount") "\D" ,"10.254.50.6") > 0
Also:

Code: Select all

find   = 10.254.50.6
found := Instr("`n" text, "`n" find) > 0
MsgBox, 64, Result for %find%, % found ? "True" : "False"
Nice try @mikeyww
but its still fail to count correctly,
try with both "10.254.50.6"
result is incorrect: 1 found

Code: Select all

text=
(join`r`n
1
2
2
3
4
10.2.10.2
10.254.50.60
10.254.50.61
10.254.50.6
10.254.50.6
10.10.10.10
10.2.1.100
)
about the 2nd method:
It does not perform a count :(
User avatar
mikeyww
Posts: 5342
Joined: 09 Sep 2014, 18:38

Re: Help with library: TF.ahk

21 Jan 2021, 07:37

I thought that you wanted a boolean result. Below is a line count.

Code: Select all

find = 10.254.50.6
MsgBox, 64, Count for %find%, % lineCount(text, find)

lineCount(str, find) {
 StrReplace("`n" str "`r", "`n" find "`r",, count)
 Return count
}
User avatar
Tomer
Posts: 336
Joined: 21 Aug 2016, 05:11

Re: Help with library: TF.ahk

21 Jan 2021, 07:48

mikeyww wrote:
21 Jan 2021, 07:37
I thought that you wanted a boolean result. Below is a line count.

Code: Select all

find = 10.254.50.6
MsgBox, 64, Count for %find%, % lineCount(text, find)

lineCount(str, find) {
 StrReplace("`n" str "`r", "`n" find "`r",, count)
 Return count
}
works very well,
thanks!

i will still not accept this answer in forum option just to let someone else share solution with tf.ahk as well.
User avatar
mikeyww
Posts: 5342
Joined: 09 Sep 2014, 18:38

Re: Help with library: TF.ahk

21 Jan 2021, 08:02

Code: Select all

find   = 10.254.50.6
count := StrSplit(TF_Find(text,,, "^" StrReplace(find, ".", "\.") "$", 0, 0), ",").Count()
MsgBox, 64, Results for %find%, %count%
User avatar
boiler
Posts: 6868
Joined: 21 Dec 2014, 02:44

Re: Help with library: TF.ahk

21 Jan 2021, 08:17

TF.ahk simply uses StringReplace to count the number of replacements (below). There is nothing with a \D that would change that. In fact, there is no \D option anywhere in TF.ahk that I can see. Where did you get that?

Code: Select all

TF_Count(String, Char)
	{
	StringReplace, String, String, %Char%,, UseErrorLevel
	Return ErrorLevel
	}
User avatar
Tomer
Posts: 336
Joined: 21 Aug 2016, 05:11

Re: Help with library: TF.ahk

21 Jan 2021, 09:48

mikeyww wrote:
21 Jan 2021, 08:02

Code: Select all

find   = 10.254.50.6
count := StrSplit(TF_Find(text,,, "^" StrReplace(find, ".", "\.") "$", 0, 0), ",").Count()
MsgBox, 64, Results for %find%, %count%
Nice!

2 things to resolve please:

1. when there nothing was found - the result is still "1" which should be "0"
2. i changed the file format and i want to make the count from the column "IPAddress" only (6th column)

Thanks!

Code: Select all

#Include tf.ahk

text=
(join`r`n
DisplayName,Username,Office,SNIFSITECODE,ComputerName,IPAddress,Date,Time,OSVersion,SESSIONNAME,SentCount
Mike,Mike1,London,None,TESTPC,10.254.50.6,21\01\2021,16:38:16,WIN_7,console,1
Mike,Mike1,London,None,TESTPC,10.254.50.6,21\01\2021,16:38:16,WIN_7,console,2
)

FileDelete, Test_TestFile.csv
FileAppend, % text, Test_TestFile.csv


find   = 10.254.50.6
count := StrSplit(TF_Find(text,,, "^" StrReplace(find, ".", "\.") "$", 0, 0), ",").Count()
MsgBox, 64, Results for %find%, %count%

just me
Posts: 7747
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Help with library: TF.ahk

21 Jan 2021, 10:27

If this is the actual file format

Code: Select all

DisplayName,Username,Office,SNIFSITECODE,ComputerName,IPAddress,Date,Time,OSVersion,SESSIONNAME,SentCount
Mike,Mike1,London,None,TESTPC,10.254.50.6,21\01\2021,16:38:16,WIN_7,console,1
Mike,Mike1,London,None,TESTPC,10.254.50.6,21\01\2021,16:38:16,WIN_7,console,2
and you want to use the TF lib to count the occurences of a complete IP address this might do it:

Code: Select all

...
Find := "10.254.50.6"
Count := TF_Count(FileContents, "," . Find . ",")
User avatar
Tomer
Posts: 336
Joined: 21 Aug 2016, 05:11

Re: Help with library: TF.ahk

21 Jan 2021, 11:45

just me wrote:
21 Jan 2021, 10:27
If this is the actual file format

Code: Select all

DisplayName,Username,Office,SNIFSITECODE,ComputerName,IPAddress,Date,Time,OSVersion,SESSIONNAME,SentCount
Mike,Mike1,London,None,TESTPC,10.254.50.6,21\01\2021,16:38:16,WIN_7,console,1
Mike,Mike1,London,None,TESTPC,10.254.50.6,21\01\2021,16:38:16,WIN_7,console,2
and you want to use the TF lib to count the occurences of a complete IP address this might do it:

Code: Select all

...
Find := "10.254.50.6"
Count := TF_Count(FileContents, "," . Find . ",")
I don't know how,
but it works like a charm :dance:

edit:
how you read the 6th column ?
i may have more columns to come...
just me
Posts: 7747
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Help with library: TF.ahk

22 Jan 2021, 07:24

It includes the surrounding commas into the search. ;)

I don't know much about the TF lib, but it might contain functions to split a line into columns.
ahk7
Posts: 336
Joined: 06 Nov 2013, 16:35

Re: Help with library: TF.ahk

22 Jan 2021, 14:16

You could also use TF_Find first as that does allow you to use RegEx and for example \b ("word boundary") have it return all lines, and then count the commas and add one to get the total number of occurrences - here an additional space is added in one of the lines but is still counted

Code: Select all

(join`r`n
DisplayName,Username,Office,SNIFSITECODE,ComputerName,IPAddress,Date,Time,OSVersion,SESSIONNAME,SentCount
Mike,Mike1,London,None,TESTPC,10.254.50.6 ,21\01\2021,16:38:16,WIN_7,console,1 ; we count this one too
Mike,Mike1,London,None,TESTPC,10.254.50.6,21\01\2021,16:38:16,WIN_7,console,2
Mike,Mike1,London,None,TESTPC,10.254.50.61,1\01\2021,16:38:16,WIN_7,console,2 ; but skip this one
)

FileDelete, Test_TestFile.txt
FileAppend, % text, Test_TestFile.txt

find:="10.254.50.6"
Lines:=TF_Find("Test_TestFile.txt", ,, "\b" find "\b",0)
Count:=TF_Count(lines,",") + 1
; Count:=TF_Count(TF_Find("Test_TestFile.txt", ,, "\b" find "\b",0),",") + 1 ; one line
MsgBox % Count
The above looks like CSV and although you can use TF it might be finicky as not columns will line up.
If you know the linenumber(s) - which you now do if you use TF_Find, you could simple read those lines
Nnote you can read them in one go using TF_Find as well) and then Loop, parse, CSV or StrSplit to get the 6th column, see TF_ReadLines() to read specific lines, you can pass on the result of the TF_Find above. But you can get the text at the some time if you use TF_Find()

Code: Select all

LinesWithText:=TF_Find("Test_TestFile.txt", ,, "\b" find "\b",0,1) ; note the 1 for ReturnText parameter - https://github.com/hi5/TF#TF_Find
MsgBox % LinesWithText
which you can now parse with a loop, parse, `n + StrSplit

If you need to work with CSV a lot - there is a lib for that https://github.com/hi5/CSV :)
User avatar
Tomer
Posts: 336
Joined: 21 Aug 2016, 05:11

Re: Help with library: TF.ahk

24 Jan 2021, 08:00

@ahk7
Thanks for the explanation!

Return to “Ask For Help”

Who is online

Users browsing this forum: alexsu, Bing [Bot], Google [Bot], jrachr, mikeyww, payaAHK, Plikis and 56 guests