Sum values

2 snippets across 2 stacks — Bash & Linux, SQL

SHBash & Linux

Column-Based Text Processing

SH · Text Processing
syntax
awk 'pattern { action }' file
example
awk '{print $1, $4}' access.log
awk -F',' '$3 > 1000 {print $1, $3}' sales.csv
awk '{sum += $5} END {print "Total:", sum}' report.tsv
output
192.168.1.40 [28/Mar/2026:10:15:23
Alice 2340
Total: 87450

Note By default, awk splits on whitespace. -F sets a custom delimiter. $0 is the whole line, $1 is the first field, NR is the line number, NF is the number of fields. BEGIN runs before processing; END runs after.

SQLSQL

SUM and AVG

SQL · Aggregation
syntax
SELECT SUM(column), AVG(column) FROM table;
example
SELECT
  SUM(total_amount) AS revenue,
  AVG(total_amount) AS avg_order_value
FROM orders
WHERE order_date >= '2025-01-01';
output
-- revenue   | avg_order_value
-- 245890.50 | 163.93

Note Both SUM and AVG ignore NULL values. If all values are NULL, SUM returns NULL (not 0). Wrap with COALESCE(SUM(col), 0) if you need a zero default.