A numeric column in a text file or command output often needs a single total, whether it holds amounts, counts, or durations. awk adds up that column by reading one numbered field with the separator the data actually uses, skipping a header row only when one exists.

awk reads each input line as a record and splits it into fields on the -F separator, or on runs of spaces and tabs when no separator is set. Adding one field to a running variable on every line and printing it from an END block produces the total. A NR > 1 test leaves out the first line when it holds column names, but the same test drops a real value from data with no header.

awk does not reject text in a numeric field. A value such as N/A counts as 0 and 1,200.00 counts as 1 with no error, so check that the column holds plain numbers before trusting the total. Splitting on commas also suits only simple comma-separated text, and a CSV export that can contain quoted commas, embedded quotes, or multiline fields needs a CSV-aware parser instead.

Steps to sum a column with awk:

  1. Create a small tab-separated sample file with one header row and one numeric amount column.
    orders.tsv
    date	item	amount
    2026-06-01	hosting	12.50
    2026-06-02	domain	9.25
    2026-06-03	backup	26.00
  2. Sum the third field of every data row.
    $ awk -F '\t' 'NR > 1 { total += $3 } END { printf "%.2f\n", total }' orders.tsv
    47.75

    NR > 1 skips the header row. $3 selects the third column, counted from the left, and total += $3 adds it to the running total. The printf format keeps two decimals, because plain print turns a total such as 1234567.89 into 1.23457e+06.

  3. Change the field separator and field number for the real input format.
    $ awk -F, 'NR > 1 { total += $4 } END { printf "%.2f\n", total }' invoices.csv

    Use -F, for simple comma-separated text, -F'|' for pipe-delimited text, or omit -F when whitespace splitting is the intended behavior.

  4. Sum the first column of command output by removing the NR test.
    $ seq 1 10 | awk '{ total += $1 } END { print total }'
    55

    $1 selects the first column, and whitespace is the default separator, so no -F is needed. Any command that prints the number first can replace seq, such as du -k *.log, and a file name after the program reads a headerless file.

  5. Remove the sample file.
    $ rm orders.tsv
Share this guide