AWK: Sum of values in a column based on entries in another column [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
This question already has an answer here:
Using awk to sum the values of a column, based on the values of another column
4 answers
Need to sum of last column based on pipe delimiter
4 answers
I would like to sum numerical values listed in the third column of the table below based on entries were given in the first column of the same table. The table content is as follows:
John|Login|2
Mary|Login|10
Mary|Payroll|100
John|Login|200
John|Logout|10
Mary|Payroll|10
The expected outcome is as follows:
John|Login|202
John|Logout|10
Mary|Login|10
Mary|Payroll|110
How can I use awk
to get this output?
shell-script awk
marked as duplicate by Jeff Schaller, Goro, ñÃÂsýù÷, Romeo Ninov, RalfFriedl Sep 25 at 16:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
7
down vote
favorite
This question already has an answer here:
Using awk to sum the values of a column, based on the values of another column
4 answers
Need to sum of last column based on pipe delimiter
4 answers
I would like to sum numerical values listed in the third column of the table below based on entries were given in the first column of the same table. The table content is as follows:
John|Login|2
Mary|Login|10
Mary|Payroll|100
John|Login|200
John|Logout|10
Mary|Payroll|10
The expected outcome is as follows:
John|Login|202
John|Logout|10
Mary|Login|10
Mary|Payroll|110
How can I use awk
to get this output?
shell-script awk
marked as duplicate by Jeff Schaller, Goro, ñÃÂsýù÷, Romeo Ninov, RalfFriedl Sep 25 at 16:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Very similar to unix.stackexchange.com/questions/471251/subtotal-of-rows and other similar questions.
â Kusalananda
Sep 25 at 13:21
Certainly, awk can do math. That's not your question, though. What's your question? What is the operation?
â Jeff Schaller
Sep 25 at 13:23
Wanted to sum-up the values in the 3rd column based on the values in 1st and 2nd column.
â user279255
Sep 25 at 13:24
Please edit your question so that it's all together; thank you!
â Jeff Schaller
Sep 25 at 13:30
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
This question already has an answer here:
Using awk to sum the values of a column, based on the values of another column
4 answers
Need to sum of last column based on pipe delimiter
4 answers
I would like to sum numerical values listed in the third column of the table below based on entries were given in the first column of the same table. The table content is as follows:
John|Login|2
Mary|Login|10
Mary|Payroll|100
John|Login|200
John|Logout|10
Mary|Payroll|10
The expected outcome is as follows:
John|Login|202
John|Logout|10
Mary|Login|10
Mary|Payroll|110
How can I use awk
to get this output?
shell-script awk
This question already has an answer here:
Using awk to sum the values of a column, based on the values of another column
4 answers
Need to sum of last column based on pipe delimiter
4 answers
I would like to sum numerical values listed in the third column of the table below based on entries were given in the first column of the same table. The table content is as follows:
John|Login|2
Mary|Login|10
Mary|Payroll|100
John|Login|200
John|Logout|10
Mary|Payroll|10
The expected outcome is as follows:
John|Login|202
John|Logout|10
Mary|Login|10
Mary|Payroll|110
How can I use awk
to get this output?
This question already has an answer here:
Using awk to sum the values of a column, based on the values of another column
4 answers
Need to sum of last column based on pipe delimiter
4 answers
shell-script awk
shell-script awk
edited Sep 25 at 13:59
Goro
6,42552863
6,42552863
asked Sep 25 at 13:17
user279255
525
525
marked as duplicate by Jeff Schaller, Goro, ñÃÂsýù÷, Romeo Ninov, RalfFriedl Sep 25 at 16:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Jeff Schaller, Goro, ñÃÂsýù÷, Romeo Ninov, RalfFriedl Sep 25 at 16:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Very similar to unix.stackexchange.com/questions/471251/subtotal-of-rows and other similar questions.
â Kusalananda
Sep 25 at 13:21
Certainly, awk can do math. That's not your question, though. What's your question? What is the operation?
â Jeff Schaller
Sep 25 at 13:23
Wanted to sum-up the values in the 3rd column based on the values in 1st and 2nd column.
â user279255
Sep 25 at 13:24
Please edit your question so that it's all together; thank you!
â Jeff Schaller
Sep 25 at 13:30
add a comment |Â
Very similar to unix.stackexchange.com/questions/471251/subtotal-of-rows and other similar questions.
â Kusalananda
Sep 25 at 13:21
Certainly, awk can do math. That's not your question, though. What's your question? What is the operation?
â Jeff Schaller
Sep 25 at 13:23
Wanted to sum-up the values in the 3rd column based on the values in 1st and 2nd column.
â user279255
Sep 25 at 13:24
Please edit your question so that it's all together; thank you!
â Jeff Schaller
Sep 25 at 13:30
Very similar to unix.stackexchange.com/questions/471251/subtotal-of-rows and other similar questions.
â Kusalananda
Sep 25 at 13:21
Very similar to unix.stackexchange.com/questions/471251/subtotal-of-rows and other similar questions.
â Kusalananda
Sep 25 at 13:21
Certainly, awk can do math. That's not your question, though. What's your question? What is the operation?
â Jeff Schaller
Sep 25 at 13:23
Certainly, awk can do math. That's not your question, though. What's your question? What is the operation?
â Jeff Schaller
Sep 25 at 13:23
Wanted to sum-up the values in the 3rd column based on the values in 1st and 2nd column.
â user279255
Sep 25 at 13:24
Wanted to sum-up the values in the 3rd column based on the values in 1st and 2nd column.
â user279255
Sep 25 at 13:24
Please edit your question so that it's all together; thank you!
â Jeff Schaller
Sep 25 at 13:30
Please edit your question so that it's all together; thank you!
â Jeff Schaller
Sep 25 at 13:30
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
This command worked for me. it should work for you :
awk -F '|' '"]+= $3 ENDfor (i in a) print i, a[i]' filename | sort -k 1,1
John|Login| 202
John|Logout| 10
Mary|Login| 10
Mary|Payroll| 110
add a comment |Â
up vote
1
down vote
If you have GNU awk
installed, you don't have to use another tool sort
to set the order right, but you can do it in awk
itself. You can set the way the array traversal is done by setting a special variable PROCINFO["sorted_in"]
. See Using Predefined Array Scanning Orders with gawk
In your case though, you can set it to sort by the index in ascending order
awk -vFS="|" -vOFS="|" '
primaryKey=($1 FS $2)
db[primaryKey]+=$3; next
END
PROCINFO["sorted_in"] = "@ind_str_asc"
for(key in db)
print key, db[key]
' file
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
This command worked for me. it should work for you :
awk -F '|' '"]+= $3 ENDfor (i in a) print i, a[i]' filename | sort -k 1,1
John|Login| 202
John|Logout| 10
Mary|Login| 10
Mary|Payroll| 110
add a comment |Â
up vote
5
down vote
accepted
This command worked for me. it should work for you :
awk -F '|' '"]+= $3 ENDfor (i in a) print i, a[i]' filename | sort -k 1,1
John|Login| 202
John|Logout| 10
Mary|Login| 10
Mary|Payroll| 110
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
This command worked for me. it should work for you :
awk -F '|' '"]+= $3 ENDfor (i in a) print i, a[i]' filename | sort -k 1,1
John|Login| 202
John|Logout| 10
Mary|Login| 10
Mary|Payroll| 110
This command worked for me. it should work for you :
awk -F '|' '"]+= $3 ENDfor (i in a) print i, a[i]' filename | sort -k 1,1
John|Login| 202
John|Logout| 10
Mary|Login| 10
Mary|Payroll| 110
edited Sep 25 at 14:10
GAD3R
23.2k164896
23.2k164896
answered Sep 25 at 13:50
Shervan
1839
1839
add a comment |Â
add a comment |Â
up vote
1
down vote
If you have GNU awk
installed, you don't have to use another tool sort
to set the order right, but you can do it in awk
itself. You can set the way the array traversal is done by setting a special variable PROCINFO["sorted_in"]
. See Using Predefined Array Scanning Orders with gawk
In your case though, you can set it to sort by the index in ascending order
awk -vFS="|" -vOFS="|" '
primaryKey=($1 FS $2)
db[primaryKey]+=$3; next
END
PROCINFO["sorted_in"] = "@ind_str_asc"
for(key in db)
print key, db[key]
' file
add a comment |Â
up vote
1
down vote
If you have GNU awk
installed, you don't have to use another tool sort
to set the order right, but you can do it in awk
itself. You can set the way the array traversal is done by setting a special variable PROCINFO["sorted_in"]
. See Using Predefined Array Scanning Orders with gawk
In your case though, you can set it to sort by the index in ascending order
awk -vFS="|" -vOFS="|" '
primaryKey=($1 FS $2)
db[primaryKey]+=$3; next
END
PROCINFO["sorted_in"] = "@ind_str_asc"
for(key in db)
print key, db[key]
' file
add a comment |Â
up vote
1
down vote
up vote
1
down vote
If you have GNU awk
installed, you don't have to use another tool sort
to set the order right, but you can do it in awk
itself. You can set the way the array traversal is done by setting a special variable PROCINFO["sorted_in"]
. See Using Predefined Array Scanning Orders with gawk
In your case though, you can set it to sort by the index in ascending order
awk -vFS="|" -vOFS="|" '
primaryKey=($1 FS $2)
db[primaryKey]+=$3; next
END
PROCINFO["sorted_in"] = "@ind_str_asc"
for(key in db)
print key, db[key]
' file
If you have GNU awk
installed, you don't have to use another tool sort
to set the order right, but you can do it in awk
itself. You can set the way the array traversal is done by setting a special variable PROCINFO["sorted_in"]
. See Using Predefined Array Scanning Orders with gawk
In your case though, you can set it to sort by the index in ascending order
awk -vFS="|" -vOFS="|" '
primaryKey=($1 FS $2)
db[primaryKey]+=$3; next
END
PROCINFO["sorted_in"] = "@ind_str_asc"
for(key in db)
print key, db[key]
' file
answered Sep 25 at 14:09
Inian
2,900822
2,900822
add a comment |Â
add a comment |Â
Very similar to unix.stackexchange.com/questions/471251/subtotal-of-rows and other similar questions.
â Kusalananda
Sep 25 at 13:21
Certainly, awk can do math. That's not your question, though. What's your question? What is the operation?
â Jeff Schaller
Sep 25 at 13:23
Wanted to sum-up the values in the 3rd column based on the values in 1st and 2nd column.
â user279255
Sep 25 at 13:24
Please edit your question so that it's all together; thank you!
â Jeff Schaller
Sep 25 at 13:30