Artificial intelligent assistant

How to preserve order of appearance when counting words in a word list file I have two files: * `file1` contains a list of unique words * `file2` contains several sentences I want to output a tab separated file with the occurrence of each word in listed in `file1` in `file2` _while preserving the order in which they are listed in file 1_. For example: * `file 1`: dog apple cat * `file 2`: the dog played with the cat and the cat was white. the boy ate the apple. * Desired output: dog 1 apple 1 cat 2 I tried existing answers in the community, but they all sort the output.

Using any POSIX awk in any shell on every Unix box:


$ cat tst.awk
BEGIN { OFS="\t" }
NR==FNR {
words[NR] = $1
next
}
{
$0 = " " $0 " "
gsub(/[^[:alpha:]]+/," ")
for ( i in words ) {
word = words[i]
cnts[word] += gsub(" "word" ","&")
}
}
END {
for ( i=1; i in words; i++ ) {
word = words[i]
print word, cnts[word]+0
}
}



$ awk -f tst.awk file1 file2
dog 1
apple 1
cat 2


The above assumes that "word"s are all alphabetic characters and that you want the matches to be case-sensitive or the input is all lower case as in your example and that the words in file1 are unique as in your example.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy d06ae40f50edfb6b0bf3761dda2dddb3