Awk tutorial #17
-
Hi! Thank you for putting together this fantastic introduction. For the last challenge on the tutorial, I feel like I'm close, but the sandbox is unsatisfied.
I'm sure I've made a silly mistake somewhere, but any help would be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi @code-chai, Thanks for the kind words! This exercise is a little tricky (and probably not a great fit for an awk one-liner haha) If I modify your code to output the denominator, I see it's always awk -F "\t" '\
{if($3 ~/Burrito/ && $4 ~/Guacamole/) counts[$3] += 1;\
else denom += 1}\
END {for(k in counts) print(k, denom)}' orders.tsv
Steak Burrito 4234
Veggie Burrito 4234
Barbacoa Burrito 4234
Chicken Burrito 4234
Burrito 4234
Carnitas Burrito 4234 Also, you'll want to always increment the denominator and not have it under an Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
after one week, I think i got it, awk -F "\t" '{if($3 ~/Burrito/ && $4 ~/Guacamole/) countsa[$3]+=$2; if($3 ~/Burrito/) countsb[$3]+=$2} END {for (k in countsa) print (k, countsa[k]/countsb[k])}' orders.tsv > burritos_guac.tsv |
Beta Was this translation helpful? Give feedback.
Hi @code-chai,
Thanks for the kind words! This exercise is a little tricky (and probably not a great fit for an awk one-liner haha)
If I modify your code to output the denominator, I see it's always
4234
, whereas we want the denominator to be different for every burrito, since we want to know the percentage of burritos of each type that contain guac (i.e.denom
will also need to be an array).Also, you'll want to always increment the d…