Originally Posted by castor0troy
sorry guys.let me explain my requirement better.
input.txt is a million keywords
i want a grep -c "keyword1" file.txt for all keywords in input.txt and save 'number of occurences of each keyword in the file'
so output.txt will be
Well, that's what my first example does. I can imagine that a search of a million keywords takes a while to sort, though. How large is the file you search in? How many occurrences total do you expect to be there? millions? more? In my example, I use grep -f, which searches for all the keywords in the same time. That's much faster than grep
ping million times for each keyword. On the other hand, my solution then sorts all the found occurences so I can use uniq -c
on the result. That might be quite slow if there's a lot of matches.
The solution provided by mddesai
is less efficient to grep
, but it does not have to sort the output. Just modify it to
while read line
grep -c "$line" file.txt
done <input.txt >output.txt
If you want a more efficient solution, I would try awk
, but it would be more complicated.