0

Let's say I have a file DATA with 10,000,000 lines. I have another file IDS with 100,000 strings. I want to extract all lines from DATA that contain one of the strings from IDS. An additional condition is that there is a 1:1 relationship between the files, so every ID has one line of DATA and every DATA has one ID.

What is the most efficient, least complicated way to do this using standard linux command-line utilities?

My ideas so far:

  1. Build a huge regex and use grep (easy, may exceeed some limit within grep)
  2. Go through IDS line by line and grep DATA for each string separately, merge results. (easy, probably very inefficient)
  3. Build a hashmap of IDS in python, loop through DATA, extract ID and check against hash map (a bit harder)
2
  • 3
    4. man join 5. Use a real database Commented Feb 27, 2013 at 18:55
  • I am trying to use join now (which should be exactly what I need) but I am running into some trouble with sorting my data: stackoverflow.com/questions/15133894/… Commented Feb 28, 2013 at 11:06

2 Answers 2

3
grep -F -f IDS DATA

Don't miss -F: it prevents from interpreting IDS as regular expressions, and enables a much more efficient Aho-Korasick algorithm.

Sign up to request clarification or add additional context in comments.

Comments

2

If IDS contains the exact strings you need to find in DATA, one string per line, try using

grep --file=IDS DATA > results

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.