Link to home
Start Free TrialLog in
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
Avatar of Qlemo
Qlemo
Flag of Germany image

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

Avatar of Nick Collins
Nick Collins

ASKER

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
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Don't give me any points for this.
Missing a hyphen ("-") in Write-Output in line 3.
The grand total on the bottom is not working of the powershell is not correct
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