Followers 0

# Count the number of occurrences in a text file / array

## 14 posts in this topic

Hey All,

I would like to count the number of times a word shows up in a text file. Say you have a text file that contains:

Up, this works

Down, this doesn't work

Up, this works

Up, this works

Down, this doesn't work

etc..

I could like to count the number of times 'Up' occurs and the number of times 'Down' occurs. I've tried several things like reading the file to an array and breaking out the 'Up' part but can't seem to get it total the number. It just says it's '1' and doesn't increment.

Let me know what I'm doing wrong here

Dim $v_sw_down = 0 Dim$v_sw_up = 0

_FileReadToArray($input_file,$s_swportlist)
;_ArrayDisplay($s_swportlist, "Switch Ports") ; Reads the switch port status For$x = 2 to UBound($s_swportlist) -2$sw_portline = $s_swportlist[$x]

$a_sw_portline = StringSplit($sw_portline," ", 1)

; Figures out switch state
Msg (4096, "DOWNs", "You have this many occurences of DOWN:" & $aArrayDOWN, 5) Good Luck! #### Share this post ##### Link to post ##### Share on other sites $file = FileOpenDialog("Select your file",@DesktopDir,"Text document (*.txt)")
$data = FileRead($File, 1024)
$upscount = 0$Downscount = 0

$lines = StringSplit($data, @lf)
If IsArray($lines) Then$linecount = $lines[0] Else$linecount = 0
Endif

msgbox(0,"File data",$linecount) For$i = 1 to $linecount If StringinStr(FileReadLine($file, $i), 'up') Then$upscount = $upscount + 1 EndIf If StringinStr(FileReadLine($file, $i), 'down') Then$downscount = $downscount + 1 EndIf Next msgbox(0,"File data",$data)
msgbox(0,"Ups/Downs returned","Ups: " & $upscount & @LF & "Downs: " &$downscount)

Its wierd, try to understand whats going on... I didnt but it worked LOL

I used this txt file and it returned 5 up / 6 down:

up
down
up
up
down
up
down
down
down
left
right
up
down

##### Share on other sites

************ not tested

#include <File.au3>

Dim $v_sw_down = 0 Dim$v_sw_up = 0
Dim $s_swportlist _FileReadToArray($input_file, $s_swportlist) ;_ArrayDisplay($s_swportlist, "Switch Ports")

; Reads the switch port status
For $x = 1 To UBound($s_swportlist) - 1
If StringInStr($s_swportlist[$x], "Down") Then
$v_sw_down = ($v_sw_down + 1)
EndIf
If StringInStr($s_swportlist[$x], "Up") Then
$v_sw_up = ($v_sw_up + 1)
EndIf
Next

MsgBox(0, "", "Down Ports " & $v_sw_down & "/ Up Ports " &$v_sw_up)

8)

##### Share on other sites

#5 ·  Posted (edited)

That works valuater

Heres a little include:

;===============================================================================
;
; Description:      Counts the number lines that contain a certain string in a file
; Syntax:           _FileCountOccurrences($sFile,$sString)
; Parameter(s):     $sFile - File to be examined ;$sString            - String to be searched for
; Requirement(s):   None
; Return Value(s):  Success             - Returns number of occurences
;                   Failure             - Sets @error to 1 and returns 0
; Author(s):        Rad (RadleyGH at GMail dot com)
; Note(s):          None
;
;===============================================================================
Func _FileCountOccurrences($sFile,$sString)
Local $data,$stringcount, $lines,$linecount, $i$data = FileRead($sFile)$stringcount = 0

$lines = StringSplit($data, @lf)
If IsArray($lines) Then$linecount = $lines[0] Else$linecount = 0
Endif

For $i = 1 to$linecount
If StringinStr(FileReadLine($sFile,$i), $sString) Then$stringcount = $stringcount + 1 EndIf Next If$StringCount > 0 Then

FileRead($File,... they dont match... also FileRead($sFile, FileGetSize($sFile)) ???? 8) #### Share this post ##### Link to post ##### Share on other sites #9 · Posted (edited) You give the file with _FileCountOccurrences and then it reads it FileGetSize(): Returns the size of a file in bytes. Not the count Omg FileRead($file[, $count])$count = [optional] The number of characters to read. Default read the entire file.

Just remove the count completely

##### Share on other sites

rad... study the first two lines of my last post

8)

Oh I get it ^^

I was using $File = FileOpenDialog in the test script, which FileRead($File) used that, it didnt convert to $sFile, so it still worked updated #### Share this post ##### Link to post ##### Share on other sites #12 · Posted (edited) Func _FileCountOccurrences($sFile, $sString,$vCase = 0)
If FileExists($sFile) Then$sFile = FileRead($sFile) StringReplace($sFile, $sString, '',$vCase)
Return @extended
EndFunc

Edit:

Forgot a parameter.

Edited by SmOke_N

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.

##### Share on other sites

Common sense plays a roll in the basics of understanding AutoIt...

well... maybe someday i will have that

lol

8)

##### Share on other sites

well... maybe someday i will have that

lol

8)

Nah, sometimes we just all just get caught up in one way of doing it. Gary and Jdeb are good at doing that to me!

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.

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