0

Can you please guide me, on how to group and aggregate the below value using a shell script (using awk) or guide me on which is the best option?

Input textfile:

900|1000|currentaccount|usd|66.66|gbp|51.92|creditleg|121|888|999
900|1000|currentaccount|usd|77.66|gbp|71.92|creditleg|121|888|999
900|1000|currentaccount|usd|85.66|gbp|65.92|creditleg|121|888|999
900|1000|currentaccount|usd|66.66|gbp|51.92|creditleg|121|888|999
900|1000|currentaccount|usd|6.66|gbp|1.92|creditleg|121|888|999

My output should be as follows:

900|1000|currentaccount|usd|133.32|gbp|103.84|creditleg|121|888|999
900|1000|currentaccount|usd|77.66|gbp|71.92|creditleg|121|888|999
900|1000|currentaccount|usd|85.66|gbp|65.92|creditleg|121|888|999
900|1000|currentaccount|usd|6.66|gbp|1.92|creditleg|121|888|999

It aggregated and group the 1 and 4 because both are same vale, if any difference it should not aggregate.

Kindly guide thanks in advance.

2
  • 2
    It seems a bit odd that you want to sum the fields that you use for grouping. What if there was another entry that had 133.32 as its 5th field and 103.84 as its 7th field? Should that be added to the resulting entry? If you are summing currencies, isn't there a danger that the amount in Pound Sterling or US Dollars potentially are rounded wrongly?
    – Kusalananda
    Apr 24 at 6:34
  • Obviously, no results can be output until all records are read (the group trigger could occur on the first and last rows). Are there any constraints on the order of output? For example, you show the fourth row being added into the first row. This would probably require all the data in memory at once -- is the input huge (e.g. > 500MB). And are 30, 30.0 and 30.00 grouped? Apr 24 at 12:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.