Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hey all I am trying to chop the last part of a URL off regardless of it's size and I can't seem to figure it out. Below is an example of what I am trying to do.
Ah, regular expressions, so powerful yet so potentially unreadable
For the benefit of anyone who's eyes are popping at the examples above, most examples of sed use / for the delimiter but you can use a different character instead. pixellany has stuck with / and escaped the instances of / in the pattern being matched. I went with ! so as not to have to escape the instances of / in the pattern and Tinkster has used @, presumably for the same reason.
Thank you everybody for you suggestions. With a combination of what I read on this thread plus a suggestion i co-worker made i have come up with this.
echo "http://foo.bar.com/test/test2" | sed -r 's@(.*)/[^/]+/*$@\1/@'
I still don't fully understand how this regex is working. I do know however that the @ signs are replacing the /'s that you would normally use in sed. I also know that ^ stand for beginning of line and $ stands for end of line and \1 is the text to replace with and the slash after the 1 is to put the trailing slash back on.
Can somebody give me a brief description as to how the rest of this works?
Just a more verbose (possible slower and in retrospect less elegant) version
of what the other guys did.
You match the string excluding the last chunk, be it alpha or terminated
with a /, and replace the whole thing with the matched bit at the front.
The parenthesis are the capturing bit.
"^" means beginning of line---unless of course it means negation....
"[^/]+" = " at least one character that is NOT a forward slash (Extended Regex rules)
"[^\/]*" = " any number of characters that is NOT a forward slash (Standard Regex rules when "/" is the sed s delimiter)
I just realized 2 reasons why Tink's version is better than mine:
Mine will match two "/" at the end of the line
Mine will NOT match--eg--.../stuff (ie it fails if the final / is missing
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.