mikedu95
mikedu95

Reputation: 1736

grep and sort using a pattern file

I have two txt files: patterns.txt and fulldb.txt

I want to output the lines of fulldb.txt which contains a line in patterns.txt so I do:

fgrep -f patterns.txt fulldb.txt > output.txt

This works.

But now I want to have the output sorted like in the order found in patterns.txt. I can do:

copy /y NUL output_sorted.txt > NUL
for /F "tokens=*" %%J in (patterns.txt) do (
    fgrep "%%J" fulldb.txt >> output_sorted.txt
)

But this takes too much time at runtime when the files are big enough.

Any better solution ?

Thanks

Upvotes: 2

Views: 412

Answers (1)

rapidex
rapidex

Reputation: 51

i see you're on cmd, but you said in comments that a bash solution is fine by you,

the following would probably work:

while read pat; do fgrep "${pat}" fulldb.txt >> output_sorted.txt; done < patterns.txt

Upvotes: 2

Related Questions