Hi,
Is there any way I can use sort inside awk.
i.e.
awk '
{
if (NF==2) {ar[$2]+=$1};
print
}
END{
print("total"
for(a in ar) {print(ar[a],a) }
}'
I want the for loop output to be sorted.
test input
----------------------------
Sams Dept Store
3 apple
4 banana
John
5 orange
3 apple
6 banana
----------------------------
Expected Output
Sams Dept Store
3 apple
4 banana
John
5 orange
3 apple
6 banana
total
5 orange
6 apple
10 banana
Is there anyway I can do the sorting inside awk
Cheers,
Pravin.
Is there any way I can use sort inside awk.
i.e.
awk '
{
if (NF==2) {ar[$2]+=$1};
}
END{
print("total"
for(a in ar) {print(ar[a],a) }
}'
I want the for loop output to be sorted.
test input
----------------------------
Sams Dept Store
3 apple
4 banana
John
5 orange
3 apple
6 banana
----------------------------
Expected Output
Sams Dept Store
3 apple
4 banana
John
5 orange
3 apple
6 banana
total
5 orange
6 apple
10 banana
Is there anyway I can do the sorting inside awk
Cheers,
Pravin.