1

This question already has an answer here:

If I have a file words_of_interest.txt with one word per line, is there a way to use awk (or some other *nix tools) to obtain the number of times each of these words occurs in another text file my_text.txt, using only one pass?

Currently I am grep -c'ing the text for each word, but this is quite slow because the text is large, and there are several hundred words to search for.

EDIT: providing sample input and output:

[words_of_interest.txt]
joe
hi

[my_text.txt]
hi joe
hi jack
nice day today

[output]
joe 1
hi 2

EDIT2: To those who marked this question as duplicate: the question you point to is about counting all the words, whereas this one is about counting only the instances of a specific pre-defined set of words.

marked as duplicate by muru, jofel, Anthon, jimmij, Gilles Dec 10 '14 at 22:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Please provide sample input/output – Gilles Quenot Dec 10 '14 at 18:39
  • May be this helps: delorie.com/gnu/docs/gawk/gawk_204.html – mkc Dec 10 '14 at 18:58
  • @Ketan indeed that is helpful for a start, but it would need to be adapted to count only the specific words in words_of_interest.txt – mitchus Dec 11 '14 at 12:20
  • @muru do you agree with my EDIT2? – mitchus Dec 12 '14 at 11:19
  • @jofel do you agree with my EDIT2? – mitchus Dec 12 '14 at 11:32
1

For simplistic scenarios involving fixed string search and space-separated words in my_text.txt, GNU awk might work, although output order may not match that of words_of_interest.txt

awk 'NR == FNR{a[$0]; next}; $0 in a{b[$0]++}; 
    END{for (k in b) print k, b[k]}' words_of_interest.txt RS='[[:space:]]+' my_text.txt
hi 2
joe 1
  • Thanks for the reply. This doesn't output anything on my Ubuntu 14.04. – mitchus Dec 12 '14 at 12:53
  • Oddly, it works now that I installed the gawk package (although I use the awk command) – mitchus Dec 12 '14 at 13:23
  • @mitchus, great. Yes, Ubuntu has mawk by default. RS='[[:space:]]+' is GNU awk-specific – iruvar Dec 12 '14 at 13:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.