Avatar of Nick Collins
Nick Collins asked on

Count on Txt File

Good Morning,

Just a simple request I have a text file that has various header records and I want to be able to do a count on the header record ‘Order Number’.

Then output to a text file and sorted in A to Z with a total at the bottom. Example below

Order Number.        Total

GB088776               10
VB66666.                 12
HF56778.                 6

Total                          28
PowershellVB Script

Avatar of undefined
Last Comment
Qlemo

8/22/2022 - Mon
Qlemo

Assuming the text file is comma seperated (or using another delimiter):
$data = Import-CSV c:\input.csv | Group-Object 'Order Number' | Sort Name
$total =($data.Count | measure-object -Sum).Sum
( Write Output "Order NumnerˋtTotal"
  $data | % { Write-Output "$($_.Name)ˋt$($_.Count)" }
  Write-Output "Totalˋt$total"
) | Out-File C:\output.csv

Open in new window

ASKER
Nick Collins

I am getting an error with this code...

At D:\iauditor_exports_folder\ExportData\Run.ps1:3 char:37
+ ( Write Output "Order NumnerˋtTotal"
+                                     ~
Missing closing ')' in expression.
At D:\iauditor_exports_folder\ExportData\Run.ps1:4 char:3
+   $data | % { Write-Output "$($_.Name)ˋt$($_.Count)" }
+   ~~~~~
Unexpected token '$data' in expression or statement.
At D:\iauditor_exports_folder\ExportData\Run.ps1:6 char:1
+ ) | Out-File C:\output.csv
+ ~
Unexpected token ')' in expression or statement.
At D:\iauditor_exports_folder\ExportData\Run.ps1:6 char:3
+ ) | Out-File C:\output.csv
+   ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInExpression

Open in new window

ASKER CERTIFIED SOLUTION
Qlemo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
footech

Don't give me any points for this.
Missing a hyphen ("-") in Write-Output in line 3.
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
Nick Collins

The grand total on the bottom is not working of the powershell is not correct
Qlemo

Corrected script:
$data = Import-CSV C:\input.csv | Group-Object 'Order Number' -NoElement | Sort Name
$total =($data | measure-object Count -Sum).Sum
$( Write-Output "Order Numner`tTotal"
  $data | % { Write-Output "$($_.Name)`t$($_.Count)" }
  Write-Output "Total`t$total"
) | Out-File C:\output.csv

Open in new window