Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sed and regex to capture last part of url

Tags:

regex

linux

bash

I'm trying to make sed match the last part of a url and output just that. For example:

echo "http://randomurl/suburl/file.mp3" | sed (expression)

should give the output:

file.mp3

So far I've tried sed 's|\([^/]+mp3\)$|\1|g' but it just outputs the whole url. Maybe there's something I'm not seeing here but anyways, help would be much appreciated!

like image 453
glindste Avatar asked Mar 07 '12 17:03

glindste


2 Answers

this works:

 echo "http://randomurl/suburl/file.mp3" | sed 's#.*/##'
like image 59
Kent Avatar answered Nov 11 '22 12:11

Kent


basename is your good friend.

> basename "http://randomurl/suburl/file.mp3"
=> file.mp3
like image 41
Limbo Peng Avatar answered Nov 11 '22 11:11

Limbo Peng