This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Get middle part of a url

Hello there,

I have a file with ftp links that look like this:

> ftp.sra.ebi.ac.uk/vol1/sequence/ERZ914/ERZ914930/contig.fa.gz
> ftp.sra.ebi.ac.uk/vol1/sequence/ERZ928/ERZ928990/contig.fa.gz
> ....

I am reading them one by one with a while loop in bash and what I'm trying to do is to drop everything before and after ERZ914930. So, I only want to keep ERZ914930

I have tried the following:

basename=${line##*/} - This returns contig.fa.gz
base=${line%%/ERZ*} - This returns ftp.sra.ebi.ac.uk/vol1/sequence

with line being the iteration variable

Thanks!

unix bash

2 answers

base=`echo "${line}" | cut -d '/' -f 5`

Hello there!

Your suggestions returns ERZ914

Thank you

Another solution using regex:

$ [[ "ftp.sra.ebi.ac.uk/vol1/sequence/ERZ928/ERZ928990/contig.fa.gz" =~ ([[:alpha:]]{3}[[:digit:]]{6}) ]] && echo ${BASH_REMATCH[1]}
ERZ928990

You can use this approach to extract any pattern you wish should you want to capture more information or do more complex filtering.

Log in to answer this question.