Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

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 toquestion 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.

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.

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.

explaining why this is not a duplicate of the referred question
Source Link
mitchus
  • 205
  • 3
  • 10

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.

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

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.

sample input and output
Source Link
mitchus
  • 205
  • 3
  • 10

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

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.

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
Source Link
mitchus
  • 205
  • 3
  • 10
Loading