January 6, 2024

Multiply field’s (column’s) entries and sum them up with awk

$ awk '{ sum = sum + $2 * $3 } END { print sum }' file.txt 

You can write this command more concisely by using the abbreviation += for incrementing sum1:

$ awk '{ sum += $2 * $3 } END { print sum }' file.txt 

Let me know if this command can be shorten: https://yctct.com/contact

Maybe it is possible to use Datamash to do it.


  1. The AWK Programming Language, Second Edition, Aho, Kernighan, Weinberger↩︎


personal computing command-line interface (cli) gnu linux trisquel shell literacy office applications wiki text processing awk spreadsheet stuff in the terminal offline

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2024 by yctct is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88