#!/usr/bin/env bash # Filter out do(), don't(), and mul() # then strip out tails to get do and don # strip out mul() to get numbers grep -Po 'do\(\)|don.t\(\)|mul\([0-9]+,[0-9]+\)' input | grep -Po 'don|do|mul\([0-9]+,[0-9]+\)' | sed -E 's/^mul\(([0-9]+),([0-9]+)\)/\1 \2/g' | awk ' BEGIN { total = 0 todo = 1 } { if ($1 == "don") { todo = 0 } else if ($1 == "do") { todo = 1 } else if (todo == 1){ total += $1 * $2 } } END { print total }'