LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help with shell script "cut" syntax (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-shell-script-cut-syntax-781762/)

carters2 01-12-2010 01:49 PM

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?

Tinkster 01-12-2010 02:13 PM

This (kind of) works ...
Code:

echo http://mydomain.com/files/scott/test/ | awk 'BEGIN{FS=OFS="/"}{$(NF-1)="";NF=NF-1;print $0}'

arizonagroovejet 01-12-2010 02:20 PM

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]*/$!!'

Tinkster 01-12-2010 02:21 PM

And this
Code:

echo http://mydomain.com/files/scott/test/ | sed -r 's@(.*)/[^/]+/$@\1@'
http://mydomain.com/files/scott


pixellany 01-12-2010 02:26 PM

Code:

sed 's/[^\/]*\/$//'

arizonagroovejet 01-12-2010 02:34 PM

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.

carters2 01-12-2010 03:30 PM

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

Tinkster 01-12-2010 03:41 PM

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.

pixellany 01-12-2010 04:56 PM

"^" 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 11:20 PM.