LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Regex help (https://www.linuxquestions.org/questions/programming-9/regex-help-574957/)

Penguin of Wonder 08-05-2007 09:20 PM

Regex help
 
I'm trying to split up a URL that matches a regex expression but I can't figure it out.

For example, if this were my URL:
How would I find and then chop off just the file name? With the above example all I would want left would be
Quote:

subversion-1.4.0-i486-1.tgz
So far the best I can do is

Code:

cat packagelist |
while read line; do
echo ${line} | grep '.*[.].*$'
done

I know I'm missing awk or sed or something. Thanks in advance!

Tinkster 08-05-2007 09:28 PM

Code:

sed -r 's@.+/([^/]+)$@\1@'

Cheers,
Tink

ta0kira 08-05-2007 09:44 PM

This is what the basename call/function are designed for.
Code:

basename http://slackware.osuosl.org/slackware-11.0/slackware/d/subversion-1.4.0-i486-1.tgz
ta0kira

Penguin of Wonder 08-05-2007 10:47 PM

Thanks for the quick response guys! Helped out a lot!

makyo 08-06-2007 07:04 AM

Hi.

Also built into bash:
Quote:

${parameter#word}
${parameter##word}
The word is expanded to produce a pattern just as in pathname
expansion. If the pattern matches the beginning of the value of
parameter, then the result of the expansion is the expanded
value of parameter with the shortest matching pattern (the ``#''
case) or the longest matching pattern (the ``##'' case) deleted.

-- excerpt from man bash
As in this example:
Code:

#!/bin/sh

# @(#) s1      Demonstrate parameter expansion in bash.

set -o nounset
echo
echo "GNU bash $BASH_VERSION" >&2
echo

P=${1-"http://slackware.osuosl.org/slackware-11.0/slackware/d/subversion-1.4.0-i486-1.tgz"}

base=${P##*/}

echo " basename: $base"

exit 0

Producing:
Code:

% ./s1

GNU bash 2.05b.0(1)-release

 basename: subversion-1.4.0-i486-1.tgz

cheers, makyo


All times are GMT -5. The time now is 07:08 PM.