Given a file L with one non-negative integer per line and text file F, what would be a fast way to keep only those lines in F, whose line number appears in file L?
Example:
$ cat L.txt
1
3
$ cat F.txt
Hello World
Hallo Welt
Hola mundo
$ command-in-question -x L.txt F.txt
Hello World
Hola mundo
I'm looking for a command that can handle a file L with 500 million or more entries; file L is sorted numerically.
Note: I'm halfway through an implementation for a command-in-question
but I just wondered, whether one might be able to use some Unix tools here as well.
Update: Thank for all the answers, I learned a lot today! I would like to accept more one answer, but that's not possible.
Best Answer
With
C
omitting meaningful error messages: