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.

Frequently asked questions

How does Bash & Linux handle sum values?
This task is covered in 2 stacks on this page: Bash & Linux, SQL. The "Column-Based Text Processing" snippet in Bash & Linux uses `awk 'pattern { action }' file`.
Which command does the Bash & Linux example use?
The "Column-Based Text Processing" snippet uses `awk 'pattern { action }' file`, from the Text Processing section of the Bash & Linux cheat sheet.
Which stacks cover "sum values" on this page?
Bash & Linux, SQL. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Column-Based Text Processing": 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.