How to efficiently sum two columns in a file with 270,000+ rows in bash
awk
is probably faster, but the idiomatic bash way to do this is something like:
while read -a line; do # read each line one-by-one, into an array # use arithmetic expansion to add col 1 and 2 echo "$(( ${line[0]} + ${line[1]} ))"done < <(grep -v READ input.txt)
Note the file input file is only read once (by grep
) and the number of externally forked programs is kept to a minimum (just grep
, called only once for the whole input file). The rest of the commands are bash
builtins.
Using the <( )
process substition, in case variables set in the while loop are required out of scope of the while loop. Otherwise a |
pipe could be used.
Your question is pretty verbose, yet your goal is not clear. The way I read it, your numbers are on every second line, and you want only to find the maximum sum. Given that:
awk ' NR%2 == 1 {next} NR == 2 {max = $1+$2; next} $1+$2 > max {max = $1+$2} END {print max}' filename