I need to extract from a big multicolumn file (500mb to 1gb, \t separator) the first two lines containing a list of 100.000+ words appearing in a specific column.
Now I'm using a cycle like this one:
while read GREP
do
grep -m 2 "${GREP}" input.txt > output.txt ;
done < list_of_words.txt
but it takes too much time (i need to do this for many files), so i'm looking for alternatives.
a simple fgrep -f -m2 don't work since -m applies to the whole output of merged hits.
I suppose awk may be a solution but could not find help online.
For example if the input file is:
Dog Bird House
Mouse Giraffe Cat
Mouse Rhino House
Lion Horse House
Dog Rat Cat
Dog Mice Cat
I'd like to have an output file like this (Order of lines isn't relevant):
Dog Bird House
Mouse Rhino House
Mouse Giraffe Cat
Dog Rat Cat
I'm now using a list of word like this one:
House
Cat
But if the is a method to just keep the first two lines where the same word on column three would be even better! Note: words on column three will be unique and not appear in any of the other columns!