That file is actually trivial to parse. You only care about lines that have N=$species
, so you can simply do:
sed -En 's/.* ([0-9]+): N=(.*)/\1\t\2/p' speclist
That will return a tab separated list of taxID and species:
$ sed -En 's/.* ([0-9]+): N=(.*)/\1\t\2/p' speclist | head
648330 Aedes albopictus densovirus (isolate Boublik/1994)
10804 Adeno-associated virus 2
648242 Adeno-associated virus 2 (isolate Srivastava/1982)
118452 Abacion magnum
72259 Abaeis nicippe
102642 Abax parallelepipedus
392897 Abalistes stellaris
75332 Abbottina rivularis
515833 Abdopus aculeatus
56673 Antarctic bacterium DS2-3R
So to get human, for example, you could do:
$ sed -En 's/.* ([0-9]+): N=(.*)/\1\t\2/p' speclist | grep sapiens
63221 Homo sapiens neanderthalensis
9606 Homo sapiens
And, for exact matches:
$ sed -En 's/.* ([0-9]+): N=(.*)/\1\t\2/p' speclist |
awk -F'\t' '$2=="Homo sapiens"'
9606 Homo sapiens
If you also want to keep the taxon node (kingdom), you can use:
sed -En 's/.* ([A-Z]) *([0-9]+): N=(.*)/\1\t\2\t\3/p' speclist
Which prints:
$ sed -En 's/.* ([A-Z]) *([0-9]+): N=(.*)/\1\t\2\t\3/p' speclist | head
V 648330 Aedes albopictus densovirus (isolate Boublik/1994)
V 10804 Adeno-associated virus 2
V 648242 Adeno-associated virus 2 (isolate Srivastava/1982)
E 118452 Abacion magnum
E 72259 Abaeis nicippe
E 102642 Abax parallelepipedus
E 392897 Abalistes stellaris
E 75332 Abbottina rivularis
E 515833 Abdopus aculeatus
B 56673 Antarctic bacterium DS2-3R
The sed
command might appear a little daunting if you don't know sed
, but it's actually quite simple. The -E
flag enables extended regular expressions which give us a simpler syntax. The -n
suppresses normal output, so that sed
won't actually print anything unless explicitly told to.
The s/from/to/
is the substitution operator and will replace from
with to
. Here, we are replacing zero or more characters (.*
) until a space followed by one or more numbers ([0-9]+
), a :
, another space, then the string N=
and then everything else (.*
). The parentheses let us capture the matched strings, so that we can then refer to the 1st captured string as \1
, the second as \2
etc. We are therefore replacing the entire line (because the regex matches the entire line) with the 1st pattern captured (the taxID, \1
), a tab (\t
) and the species name (\2
). Finally, the p
at the end means "print the resulting line if the substitution was successful", so only lines matching the desired pattern are actually printed.