Bash: How to extract portion from the end of a line that's in a variable?
-2
votes
3
answers
243
views
In the context of a bash script, how do I extract the
uniqueID
from the uniqueID_db.dat
part of URLs such as these, where the uniqueID can be anything, for example:
https://cdn.somedomain.com/fetch/uniqueID/uniqueID_db.dat
https://server123456.eu.somedomain.com/789/storage/uniqueID/uniqueID_db.dat
https://cdn.somedomain.com/fetch/6234449e1539130b/6234449e1539130b_db.dat
https://server654321.eu.somedomain.com/0123/storage/afd85b3f9ae5bc9/afd85b3f9ae5bc9_db.dat
Lines always end in _db.dat
and it's the uniqueID before it that I'd like to extract.
Any line is in the variable $link
.
Is this extraction possible with sed
or some other tool? If so, how? And can you please explain the workings so I can learn?
I imagine something like:
echo "${link}" | sed '...'
Thanks.
Asked by algalg
(107 rep)
Dec 28, 2020, 11:17 AM
Last activity: Jun 4, 2024, 10:19 AM
Last activity: Jun 4, 2024, 10:19 AM