I've got a file that looks like this:
a 12345
b 3456
c 45678
and i've got bash array of strings:
mylist=("a" "b")
What I want to do is to sum numbers in second column but only for rows where first column value (aka "a" or "b") is present in mylist.
My not-working code:
cat myfile.txt | awk -F'\t' '{BEGIN{sum=0} {if ($1 in ${mylist[@]}) sum+=$2} END{print sum}}'
Expected result is 12345+3456=15801. I understand that problem is in if-statement but can't figure out how to rearrange this code to work.
join, perfectly well-suited to extracting only the lines you care about.