I have an awk script that generates a summary of several columns based on specific rules. I need to achieve the same on a Windows server running PowerShell v4.
TxnStat|Station|Ccy|Fcy|Lcy|Date
NEW|BRANCH|USD|1000|10|20190410
NEW|ATM|GBP|100|25|20190410
NEW|ATM|GBP|50|10|20190410
NEW|BRANCH|GBP|200|47|20190410
NEW|BRANCH|USD|250|20|20190410
The idea is to group all records that share the same Station (field 2) and Currency (field 3) and then:
- Calculate the sum of Fcy Amount for the group (field 4)
- Calculate sum of LCY amount for the group (field 5)
- Calculate field 5/field 4 (i.e. exch rate)
- Get the average exch rate (exch/no. of items per group)
The result I'm looking for is this. I am stripping out the header:
NEW,BRANCH,USD,30.00,1250.00,0.02,0.01,20190410
NEW,ATM,GBP,35.00,150.00,0.23,0.12,20190410
NEW,BRANCH,GBP,47.00,200.00,0.23,0.23,20190410
I used the awk script below to achieve this:
tail -n+2 TEST.CSV | awk 'BEGIN{FS="|";OFS=","}
{fcy_tot[$2$3]+=$4;num_recs[$2$3]+=1;
lcy_tot[$2$3]+=$5;str_1[$2$3]=$1OFS$2OFS$3;
dt_str[$2$3]=$6;}
END
{for (i in fcy_tot)
{exch=lcy_tot[i]/fcy_tot[i];
avg_rate=exch/num_recs[i];
printf "%s %.2,%.2f,%.2f,%.2f,%.2f,%s\n",
str_1[i],lcy_tot[i],fcy_tot[i],exch,avg_rate,dt_str[i]}}'
It's a one-liner but I've broken it here for clarity.
What have I done so far?
I did some Googling and landed on the group-object
function but I was only able to find the sum on one field using the measure -sum
command. I need guidance on how to create custom calculations on multiple specific fields.
Measure-Object
in PowerShell 6 (PowerShell Core) supports a more extensive set of capabilities than previous versions did, including being able to specify multiple fields for calculations, and provision of a scriptblock for calculations. See Microsoft Docs onMeasure-Object
for PS6|
to being delimited with,
; PowerShell'sImport-CSV
allows you to specify a-Delimiter
parameter since PS4.