The desired output keeps for each line the first two 'columns' and adds the number of occurrences of 'word' on that same line.
Input:
string1 string2 aaaaaaaaa word aaaaaaaa word
string3 string4 ccccccccccc word dddaaaaaaacccd word dddddaaaaa word bbbb
string5 string6 aaaa word bbbbbbaddd word aaaaa word ccccccdddddddddd word cccccc
Desired output:
string1 string2 2
string3 string4 3
string5 string6 4
Any suggestions?
Using awk
awk '{print $1,$2,gsub(/word/,"")}' file
string1 string2 2
string3 string4 3
string5 string6 4
I'm ignoring sed, here's how to do it with awk:
awk '{count=0;
for(i=3; i <= NF; i++) {if($i=="word") { count++ }};
print $1, $2, count; }' inputfile
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With