Sample Header Ad - 728x90

Filter file by line number

18 votes
10 answers
4408 views
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 than one answer, but that's not possible. I took the fastest solution from the current answers an put them into a standalone tool: [filterline](https://github.com/miku/filterline) .
Asked by miku (683 rep)
Jun 13, 2015, 10:27 AM
Last activity: Apr 17, 2023, 09:04 AM