How to get all the lines in between partial matches using awk or sed?

Viewed 65

My file looks something like this:

>Cluster 0
0   58aa, >5XX8A... at 91.38%
1   58aa, >3LDMA... at 100.00%
2   58aa, >3BTHI... at 96.55%
3   65aa, >1F7ZI... *
4   58aa, >3LDJA... at 100.00%
>Cluster 1
0   57aa, >1ZJDB... at 94.74%
1   58aa, >1AAPA... at 91.38%
2   56aa, >5NX1D... at 92.86%
>Cluster 2
0   60aa, >4ISLB... at 98.33%
1   62aa, >4ISNB... at 95.16%
>Cluster 3
0   59aa, >3BYBA... *
1   59aa, >5ZJ3A... at 100.00%
2   59aa, >3UIRC... at 100.00%
3   57aa, >3D65I... at 100.00%

How can I use sed or awk to get the IDs after > (for example: 5XX8A) in between the ">Cluster" ones. I want to be able to save them separately (in different files). One file per cluster. Or something more parsable like a single file with the IDs right next to the cluster number.

As a first approach doing something like:

sed -n '/^\>/,/^\>/p' filename 

returns the whole file :/

2 Answers

awk to the rescue!

$ awk '/^>Cluster /{close(f); f="Cluster."$2; next} {sub(/>/,"",$3); print $3 > f}' file
  
$ head Cluster*
==> Cluster.0 <==
5XX8A...
3LDMA...
3BTHI...
1F7ZI...
3LDJA...

==> Cluster.1 <==
1ZJDB...
1AAPA...
5NX1D...

==> Cluster.2 <==
4ISLB...
4ISNB...

==> Cluster.3 <==
3BYBA...
5ZJ3A...
3UIRC...
3D65I...

This might work for you (GNU sed):

sed -En '/^>(Cluster) /{s//>\1./;:a;x;s/\n(.*)/ echo "\1"/e;x;h;d};s/.*>//;s/ .*//;H;$!d;ba' file

Gather up each cluster in the hold space and using the evaluation flag on the substitution command echo the collection to the file name indicated by the first line of the collection.

Alternative method, using sed and piping to sh:

sed '/^>Cluster/{s/ /./;h;d};s/..*>//;s/ .*//;G;x;s/>*/>>/;x;s/\n/ /;s/\S*/echo "&"/' file|sh

Alternative method, using sed and csplit:

sed 's/^..*>//;s/ .*//' file | csplit -szf Cluster -b '.%d' --suppress-matched - '/>Cluster/' '{*}'

Manipulate the file into the desired format using sed and then split the file into separate files using csplit.

N.B. This may not replicate the filenames faithfully.

Related