Jonny Davy
Jonny Davy

Reputation: 55

Parsing hundreds of .txt files that contain numbers, then finding the sum

I have a script that I run on thousands of servers that compiles keywords from logs and outputs how many times they occur within a text file. I use xcopy to snag the logs straight to my box and put them all into the same folder. (IE: Within C:\scripts\ contains server1_results.txt, server2_results.txt, etc) The output looks like this for every individual server:

---------- C:\TEMP\TEXT.001: 0
---------- C:\TEMP\TEXT.002: 0
---------- C:\TEMP\TEXT.003: 2
---------- C:\TEMP\TEXT.004: 0
---------- C:\TEMP\TEXT.005: 1

The number after the colon is the number of times a keyword appears within a log. I then use a batch script to parse everything out before the final number and then add the total, this then exports the results into a new text file.

My method works fine, however, I have hundreds of logs and my script only works on one log -- one at a time.

I've been playing with Get-Content but not finding the results I'm looking for.

So, I need a script that will loop and parse every file, and find the sum total of every individual file (server1 will have its own result, server2 will have its own result). It would also be helpful if the script would add the filename to the result. The result I'm looking for:

server1_results.txt : 22

Upvotes: 0

Views: 196

Answers (3)

user6811411
user6811411

Reputation:

A powershell script quite similar to my batch answer in
Parsing multiple text files with alternate file names, finding sum, and then compiling output with Batch file

## Q:\Test\2019\04\02\SO_55475471.ps1

$Folder = 'X:\path\to\files'

$HashTable = @{}
foreach ($File in (Get-ChildItem -Path $Folder -Filter *_count.txt -File)){
  Select-String -Path $File -Pattern '(?<=-{5,}.*)\d+$' | ForEach-Object {
    $HashTable[$File.BaseName]+= [int]$_.Matches[0].Value
  }
}
$HashTable

Sample output:

> Q:\Test\2019\04\02\SO_55475471.ps1

Name                           Value
----                           -----
server1_count                  3
server2_count                  15

PS: Just change *_count.txt=> *_results.txt

To get some statistics:

> $HashTable.GetEnumerator()|Measure-Object Value -sum -ave -min -max

Count    : 2
Average  : 9
Sum      : 18
Maximum  : 15
Minimum  : 3
Property : Value

Upvotes: 0

AdminOfThings
AdminOfThings

Reputation: 25001

I believe this will accomplish what you want:

foreach ($file in (Get-ChildItem "C:\scripts\*_results.txt")) {
    $Sum = 0
    switch -regex -file $file
    {
        "[1-9]\d*$" {$sum += $matches[0] -as [int]}
    }
    "{0} : {1}" -f $file.name,$sum
}

Do you have any performance objectives?

Upvotes: 0

Bill
Bill

Reputation: 554

This works for me

$Files = (Get-ChildItem c:\scripts\*.txt).FullName
$count=0
foreach ($File in $Files)
{
    $thisCount = Get-Content $File
    $Count = $Count + $thisCount
}
"Total Count is: $Count"

Upvotes: 1

Related Questions