Skip to main content

How to Sum Column in Bash

• 2 min read
bash math data processing sum column awk

Quick Answer: Sum Column in Bash

To sum values in a column, use awk '{sum += $1} END {print sum}' file.txt where $1 is the column number. For CSV files with commas, use awk -F',' '{sum += $2} END {print sum}' file.csv. The awk method is fastest and most reliable.

Quick Comparison: Column Sum Methods

MethodDelimiter HandlingSpeedBest ForPrecision
awkFlexibleFastestAll casesGood
paste + bcComma onlyMediumCSV filesExcellent
while loopAnySlowSmall filesGood

Bottom line: Use awk for all cases; it’s the fastest and most flexible.


Add up values in a column from CSV or structured data files. Learn methods using awk, bc, paste, and bash loops.

The fastest and most practical method for all cases:

# Sum first column
awk '{sum += $1} END {print sum}' numbers.txt

# Sum specific column
awk -F"," '{sum += $2} END {print sum}' data.csv

Basic Example

Test file (sales.txt):

1500
2300
950
2100
1800
# Sum the sales column
awk '{sum += $1} END {print sum}' sales.txt

# Output: 8650

Sum Specific Column from CSV

Test file (inventory.csv):

Product,Quantity,Price
Apple,100,1.50
Banana,150,0.75
Cherry,50,2.00
# Sum quantities (column 2)
awk -F"," 'NR > 1 {sum += $2} END {print sum}' inventory.csv

# Output: 300

# Sum prices (column 3)
awk -F"," 'NR > 1 {sum += $3} END {print sum}' inventory.csv

# Output: 4.25

Sum Column from Pipe Input

# Sum values from command output
ls -la | awk '{sum += $5} END {print sum}'

# Sum process memory usage
ps aux | awk '{sum += $6} END {print sum " KB"}'

Conditional Sum

#!/bin/bash

# Sum only values matching condition

file="$1"
column="${2:-1}"
condition="${3:->0}"

if [ ! -f "$file" ]; then
  echo "Usage: $0 <file> <column> [condition]"
  exit 1
fi

# Sum column where value > condition
awk -v col="$column" -v cond="$condition" \
  '{if ($col > cond) sum += $col} END {print sum}' "$file"

Usage:

$ ./sum_conditional.sh sales.txt 1 1000
# Sums all values in column 1 greater than 1000

Practical Example: Revenue Report

#!/bin/bash

# File: revenue_report.sh

data_file="$1"

if [ ! -f "$data_file" ]; then
  echo "Usage: $0 <data_file>"
  exit 1
fi

echo "=== Revenue Report ==="
echo ""

# Sum all revenue
total=$(awk -F"," 'NR > 1 {sum += $3} END {print sum}' "$data_file")
echo "Total Revenue: \$$(printf "%.2f\n" $total)"

# Sum by category
echo ""
echo "Revenue by Product:"
awk -F"," 'NR > 1 {
  product=$1
  revenue=$3
  total[product] += revenue
}
END {
  for (p in total) {
    printf "  %s: \$%.2f\n", p, total[p]
  }
}' "$data_file"

# Average
avg=$(awk -F"," 'NR > 1 {sum += $3; count++} END {print sum/count}' "$data_file")
echo ""
echo "Average Revenue: \$$(printf "%.2f\n" $avg)"

Test file (transactions.csv):

Date,Product,Amount
2026-02-01,Apple,150.50
2026-02-02,Banana,200.75
2026-02-03,Apple,175.25
2026-02-04,Cherry,125.00

Output:

=== Revenue Report ===

Total Revenue: $651.50

Revenue by Product:
  Apple: $325.75
  Banana: $200.75
  Cherry: $125.00

Average Revenue: $162.88

Sum Using bc and paste

# Convert column to addition expression and use bc
cat numbers.txt | paste -sd+ | bc

# Example:
# Input: 1, 2, 3, 4
# paste output: 1+2+3+4
# bc output: 10

Sum in Bash Loop

For small datasets or complex logic:

#!/bin/bash

sum=0

while read value; do
  # Skip empty lines and non-numeric
  [[ $value =~ ^[0-9]+$ ]] || continue
  sum=$((sum + value))
done < numbers.txt

echo "Total: $sum"

Multi-Column Sum

#!/bin/bash

# Sum multiple columns and display results

file="$1"

if [ ! -f "$file" ]; then
  echo "Usage: $0 <file>"
  exit 1
fi

# Assume 3 columns of numbers
awk '{
  col1 += $1
  col2 += $2
  col3 += $3
}
END {
  printf "Column 1: %d\n", col1
  printf "Column 2: %d\n", col2
  printf "Column 3: %d\n", col3
  printf "Total:    %d\n", col1+col2+col3
}' "$file"

Test file (data.txt):

10 20 30
15 25 35
12 18 22

Output:

Column 1: 37
Column 2: 63
Column 3: 87
Total:    187

Sum with Floating Point

# Sum decimal values
awk '{sum += $1} END {printf "%.2f\n", sum}' prices.txt

# Sum and format with commas
awk '{sum += $1} END {printf "%'"'"'d\n", int(sum+0.5)}' values.txt

Running Total

#!/bin/bash

# Display cumulative sum

echo "Value  Running_Total"
echo "-----  -----"

total=0

while read value; do
  total=$((total + value))
  printf "%5d  %5d\n" "$value" "$total"
done < numbers.txt

Sum Multiple Files

#!/bin/bash

# Sum same column across multiple files

column="${1:-1}"
shift

total=0

for file in "$@"; do
  if [ -f "$file" ]; then
    file_sum=$(awk -v col="$column" '{sum += $col} END {print sum}' "$file")
    echo "$file: $file_sum"
    total=$((total + file_sum))
  fi
done

echo "---"
echo "Combined Total: $total"

Usage:

$ ./sum_multiple.sh 1 file1.txt file2.txt file3.txt
file1.txt: 350
file2.txt: 420
file3.txt: 380
---
Combined Total: 1150

Sum with Header Row

# Skip header (first line)
awk 'NR > 1 {sum += $1} END {print sum}' data.txt

# Skip first N lines
awk 'NR > 3 {sum += $1} END {print sum}' data.txt

Weighted Sum

#!/bin/bash

# Sum with weights (e.g., grades with credit hours)

awk -F"," '{
  total_points += $1 * $2
  total_weight += $2
}
END {
  if (total_weight > 0) {
    printf "Weighted Average: %.2f\n", total_points / total_weight
  }
}' "$file"

Test file (grades.csv):

95,3
85,4
90,2

Output:

Weighted Average: 88.89

Common Mistakes

  1. Not initializing sum - awk auto-initializes to 0
  2. Wrong field separator - use -F"," for CSV
  3. Including headers in sum - use NR > 1 to skip
  4. Losing precision - use printf "%.2f" for decimals
  5. Wrong column number - remember $1 is first column

Performance Tips

  • Awk is fastest for large files
  • Avoid bash loops for big datasets
  • Use paste | bc for simple addition
  • Pipe to sort -n before summing if needed

Key Points

  • Use awk for efficient column summing
  • Use -F to set field separator
  • Use NR > 1 to skip headers
  • Use printf for decimal formatting
  • Test with different data types

Summary

Summing columns is fundamental for data analysis. Awk provides the fastest and most reliable method. Use it with field separators for CSV files, and always handle headers and data types properly.