简体   繁体   中英

Print sums of more than one column in Linux

I want to get sums of two columns' values. I want each result separately. Here is my values:

command

iostat -x 1 2 \
| perl -e 'local $/=""; @p=<STDIN>; print @p[4];' \
| awk '{ print $6 " " $7}' \
| grep -v rsec

output

0.00 1608.00
22.00 0.00
10.00 1608.00
0.00 1312.00
0.00 0.00
0.00 0.00
0.00 296.00

I want this result

32.00 4824.00

Here is what i tried:

iostat -x 1 2 \
| perl -e 'local $/=""; @p=<STDIN>; print @p[4];' \
| awk '{ print $6 " " $7}' \
| grep -v rsec \
| awk '{ SUM += $1 SUM2 += $2} END { print SUM " " SUM2}'

This command gives me a syntax error. What's the solution for this?

According to the man page, commands in awk can be separated by newlines, semicolons, or both. Your two commands are not separated by any of those things. The solution is to write:

| awk '{ SUM += $1; SUM2 += $2} END { print SUM " " SUM2}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM