I have a text file some miscellaneous data and with some money expenses. I want to search all dollar quantities between specific lines and sum them. Specific lines meaning search for dollar quantities between lines 6 and 8.
Here's an example of my text file:
Mary had a little $5.00 lamb
Bing bang bow
Blah blah blah
STARBUCKS Jan 8th, 2019 $7.00
MCDONALD'S Jan 10th, 2019 $6.00
UBER Jan 11th, 2019 $20.01
The expected answer is $33.01
I found that in VI I can search dollar quantities like this:
/$\d\{2}\|\$\d\{1}
I also saw in my search results that AWK can search numbers and sum them, but I couldn't figure out how to tailor those suggestions to my problem.
Use $
as field separator. If there is a second column (NF==2
) sum values in second column.
awk -F '$' 'NF==2{sum+=$2} END{print sum}' file