LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 01-12-2010, 02:49 PM   #1
carters2
LQ Newbie
 
Registered: Sep 2009
Posts: 20

Rep: Reputation: 1
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?
 
Old 01-12-2010, 03:13 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
This (kind of) works ...
Code:
echo http://mydomain.com/files/scott/test/ | awk 'BEGIN{FS=OFS="/"}{$(NF-1)="";NF=NF-1;print $0}'
 
Old 01-12-2010, 03:20 PM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
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

Quote:
$ echo http://mydomain.com/files/scott/test/| sed 's![a-zA-Z0-9]*/$!!'

Last edited by arizonagroovejet; 01-12-2010 at 03:21 PM. Reason: Hit post before I'd finished
 
Old 01-12-2010, 03:21 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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.
Old 01-12-2010, 03:26 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Code:
sed 's/[^\/]*\/$//'
 
Old 01-12-2010, 03:34 PM   #6
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
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.
 
Old 01-12-2010, 04:30 PM   #7
carters2
LQ Newbie
 
Registered: Sep 2009
Posts: 20

Original Poster
Rep: Reputation: 1
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
 
Old 01-12-2010, 04:41 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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.
 
Old 01-12-2010, 05:56 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
"^" 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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
./configure script hangs at "checking for ICMP ping syntax..."l khilari Linux - Software 4 11-12-2011 11:27 AM
converting shell script from "dialog" to "xdialog" kushalkoolwal Programming 1 02-25-2008 09:40 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
"Cut & Paste" To and From Shell TatforTit Linux - Newbie 5 03-05-2006 12:34 PM
using "cut" in perl script armegeden Linux - General 3 03-06-2003 10:09 AM


All times are GMT -5. The time now is 10:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration