Hi,
I have file with below format and sample data - File is pipe delimited
Col1|col2|Account|Bal1|Bal2
1|2|1|10|5
1|2|2|10|2
1|3|3|10|3
I want output as
SUM|1|2|2|20|7
SUM|1|3|1|10|3
Can anyone give me awk command
Hi,
I have file with below format and sample data - File is pipe delimited
Col1|col2|Account|Bal1|Bal2
1|2|1|10|5
1|2|2|10|2
1|3|3|10|3
I want output as
SUM|1|2|2|20|7
SUM|1|3|1|10|3
Can anyone give me awk command
how you got the required output
This is Output
SUM|1|2|2|20|7
SUM|1|3|1|10|3
First col is fized string
Second and thrid column are col1 and col 2 from input, fouth column is recound count bases on col and col2, fifth column and six are summation on input 4 and 5 col.
Try...
$ cat file1
Col1|col2|Account|Bal1|Bal2
1|2|1|10|5
1|2|2|10|2
1|3|3|10|3
$ awk 'BEGIN{FS=OFS="|"}
NR>1{
i = $1 FS $2
a++
b+=$(NF-1)
c+=$NF
}
END{
for(i in a)
print "SUM", i, a, b, c | "sort"
}' file1 > file2
$ cat file2
SUM|1|2|2|20|7
SUM|1|3|1|10|3
$
awk -F"|" '{a[$1FS$2]++;b[$1FS$2]+=$4;c[$1FS$2]+=$5}END{for (i in a) print "SUM|"i,a,b,c}' OFS="|" infile