Let's say I have a text file with billions of text lines sorted alphabetically, like
Bar=10
Foo=6
Naz=42
How can I search for the line starting with Foo
in the most optimal way (the file contains billions of variables like this), knowing lines are sorted alphabetically and that the line I want to find must start (or "contain" if it's easier to search for) a specific text?
Edit:
This question can be considered as duplicate of https://askubuntu.com/q/423886/10473
Answer is to use look
which is fast enough for such research
=
? Will you only be searching with a single string or with many separate strings (expecting many answers)? Do you care for substring matches (so thatFoo
matches not onlyFoo
but alsoAhFoo
andFoobiz
, orHoo=Foo
etc.)? Are these variables that would be valid in a shell? Are there duplicated lines, or duplicated variable names?look
was actually what I looked for. I made it using... | xargs -I "{}" look -f "{}" "sorted.txt"
which returns the result within a second. You may make an answer if you want me to accept it and get the reputation from it ;) Thanks again!