LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-05-2007, 09:20 PM   #1
Penguin of Wonder
Senior Member
 
Registered: Sep 2005
Location: West Virginia
Distribution: Gentoo
Posts: 1,249

Rep: Reputation: 45
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!
 
Old 08-05-2007, 09:28 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
sed -r 's@.+/([^/]+)$@\1@'

Cheers,
Tink
 
Old 08-05-2007, 09:44 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 08-05-2007, 10:47 PM   #4
Penguin of Wonder
Senior Member
 
Registered: Sep 2005
Location: West Virginia
Distribution: Gentoo
Posts: 1,249

Original Poster
Rep: Reputation: 45
Thanks for the quick response guys! Helped out a lot!
 
Old 08-06-2007, 07:04 AM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

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


Reply

Tags
basename, regex



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
config using regex Sushy Linux - General 1 04-06-2007 07:43 AM
Regex in sql Reegz Programming 6 07-21-2006 08:59 AM
.NET regex \b but not / rjwilmsi Programming 0 06-21-2006 03:45 AM
regex help siyisoy Programming 4 04-07-2006 05:32 AM
Regex Help cmfarley19 Programming 5 03-31-2005 10:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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
Open Source Consulting | Domain Registration