Reputation: 83
Intake file
website1 ip 20
website1 ip 30
website1 ip 10
website2 ip 200
website2 ip 10
website3 ip 150
I need it to sort so that the sum of the 3rd with the associated website will show first with the highest total with the website
website2 has 210 website 3 has 150 website1 has 60
website2 ip 200
website2 ip 10
website3 ip 150
website1 ip 30
website1 ip 20
website1 ip 10
ive tried sort -k3n -k1n but that is not the results i am looking for 230 website2 ip 200
Upvotes: 1
Views: 351
Reputation: 785156
You can use awk to have 2 pass and sum the column 3 per website (column 1) and append a new column in the output. Then sort using new column in output and finally strip first column from output using cut
:
awk 'FNR==NR{sum[$1]+=$3; next} {print sum[$1] "\t" $0}' file file |
sort -k1nr -k4nr | cut -f2-
website2 ip 200
website2 ip 10
website3 ip 150
website1 ip 30
website1 ip 20
website1 ip 10
Output of awk
command:
awk 'FNR==NR{sum[$1]+=$3; next} {print sum[$1] "\t" $0}' file file
60 website1 ip 20
60 website1 ip 30
60 website1 ip 10
210 website2 ip 200
210 website2 ip 10
150 website3 ip 150
Upvotes: 3