Shell Script – Sum Last Column Based on Pipe Delimiter

awkshell-script

I have below inputs with huge number of rows

11|ABCD|19900101123123445455|555|AAA|50505050|0000009030
11|ABCD|19900101123123445455|555|AAA|50505050|0000000199
13|ABCD|201803010YYY66666666|600|ETC|20180300|0000084099
11|ABCD|19900101123123445455|555|AAA|50505050|0008995001

And I need to get below output

11|ABCD|19900101123123445455|555|AAA|50505050|9004230
13|ABCD|201803010YYY66666666|600|ETC|20180300|84099

I have been trying with below awk but having too limited knowledge with arrays.

cat test|awk -F"|" '{ a[$1]++;b[$2]++;c[$3]++;d[$4]++;e[$5]++;f[$6]+=$6 }; END { for (i in a); print i, f[i]}'

I need to sum last column of column number 6 and print all first 5 columns, which are separated by pipe and last 6th column as sum of 6th column.

Best Answer

Awk solution:

awk 'BEGIN{ FS=OFS="|" }
     { a[$1 FS $2 FS $3 FS $4 FS $5 FS $6] += $7 }
     END{ for (i in a) print i, a[i] }' file

The output:

11|ABCD|19900101123123445455|555|AAA|50505050|9004230
13|ABCD|201803010YYY66666666|600|ETC|20180300|84099
Related Question