Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
01-12-2010, 01:49 PM
|
#1
|
LQ Newbie
Registered: Sep 2009
Posts: 20
Rep:
|
Need help with shell script "cut" syntax
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.
If I pass in...
http://mydomain.com/files/scott/test/
I want to get out...
http://mydomain.com/files/scott/
i have tried the following and it's not working
location="http://mydomain.com/files/scott/test/"
echo $location | cut -d / -f 1-$((NF-1))
That does not work.
Does anybody have a quick one liner to do this?
|
|
|
01-12-2010, 02:13 PM
|
#2
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
This (kind of) works ...
Code:
echo http://mydomain.com/files/scott/test/ | awk 'BEGIN{FS=OFS="/"}{$(NF-1)="";NF=NF-1;print $0}'
|
|
|
01-12-2010, 02:20 PM
|
#3
|
Senior Member
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094
Rep: 
|
You can use dirname but it will lose the trailing /
Code:
$ dirname http://mydomain.com/files/scott/test/
Or you can use sed like so
Last edited by arizonagroovejet; 01-12-2010 at 02:21 PM.
Reason: Hit post before I'd finished
|
|
|
01-12-2010, 02:21 PM
|
#4
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
And this
Code:
echo http://mydomain.com/files/scott/test/ | sed -r 's@(.*)/[^/]+/$@\1@'
http://mydomain.com/files/scott
|
|
1 members found this post helpful.
|
01-12-2010, 02:26 PM
|
#5
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Code:
sed 's/[^\/]*\/$//'
|
|
|
01-12-2010, 02:34 PM
|
#6
|
Senior Member
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094
Rep: 
|
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.
|
|
|
01-12-2010, 03:30 PM
|
#7
|
LQ Newbie
Registered: Sep 2009
Posts: 20
Original Poster
Rep:
|
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?
Thanks again,
Scott
|
|
|
01-12-2010, 03:41 PM
|
#8
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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.
|
|
|
01-12-2010, 04:56 PM
|
#9
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
"^" 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
|
|
|
All times are GMT -5. The time now is 01:22 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|