The simple case you show is trivial. Your sequences are never more than a single line so you can simply use
grep to search for each of your IDs and the line after them:
grep -Fwf list.txt -A 1 seq.fasta | grep -v '^--$' > out.fasta
grep -v '^--$' simply filters out the lines with
grep adds between groups of output lines when using the
To also avoid the dupes, you can pass your list through (GNU) sort:
grep -Fwf <(sort -u list.txt) -A 1 seq.fasta | grep -v '^--$' > out.fasta
The flags used are:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file
contains zero patterns, and therefore matches nothing. (-f is
specified by POSIX.)
Select only those lines containing matches that form whole
words. The test is that the matching substring must either be
at the beginning of the line, or preceded by a non-word
constituent character. Similarly, it must be either at the end
of the line or followed by a non-word constituent character.
Word-constituent characters are letters, digits, and the
Interpret PATTERN as a list of fixed strings, separated by
newlines, any of which is to be matched. (-F is specified by
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing a group separator (--) between
contiguous groups of matches. With the -o or --only-matching
option, this has no effect and a warning is given.
However, in most cases, your sequences will be several lines and that will not be enough. If you will be doing this kind of thing often, I suggest you install the
exonerate suite of tools. They are generally extremely useful for bioinformatics work and include a nice tool called
fastafetch that is designed to do exactly what you want:
Install the exonerate suite. This is in the repositories of Debian based systems, and is also available from here.
sudo apt-get install exonerate
Create an index for your fasta file. This is used for the rapid retrieval of sequences.
fastaindex seq.fasta seq.idx
Extract your sequences:
$ fastafetch -f seq.fasta -i seq.idx -Fq <(sort -u list.txt )
As a side note, I have written a script that can do this and a bit more. It can take one or more lists of input IDs and will extract the corresponding sequences from a multi fasta file. It can either print to STDOUT or create a file for each input list. You can find it here.