I have a big file of fasta sequence and a list of IDs. I need to grep some sequences with header using their IDs from another file. Here, is the files examples.

File 1:


File 2

  • Generic programming questions are better suited for stackexchange.com
    – user692175
    Nov 14 '17 at 18:11
  • @MichaelBay Is this a generic programming question? Nov 14 '17 at 18:14
  • Which is the id file here? Nov 14 '17 at 18:16
  • @George I think it is and the OP's other question as well. So, a pattern is being shown. But if you want to answer it I'm not stopping you ;)
    – user692175
    Nov 14 '17 at 18:18
  • @George So far the OP asked two questions. So, when I say "OP's other question" it must be askubuntu.com/questions/974459/… Now, how are both questions related to Ubuntu? Answers for both are applicable to pretty much any Linux distro and beyond therefore better suited to main SE.
    – user692175
    Nov 14 '17 at 18:45

You can have grep read patterns from a file, one pattern per line, with the -f option:

grep -x -F -A 1 -f 'File 2' 'File 1'


  • -F interprets patterns literally and not as regular expressions,

  • -x only matches entire lines,

  • -A N prints N lines following each match.


I got this to work:

for i in $(cut -d" " -f1- file2); do grep -o "$i" file1 | tee -a result.txt; done

Assumption file1 == id file, and file2 == file to be greped


  • Please edit your question and show us the id file and the expected outcome Nov 15 '17 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.