LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-20-2006, 09:45 PM   #1
DriveMeCrazy
Member
 
Registered: Mar 2004
Posts: 70

Rep: Reputation: 15
SED help needed


Hi guys,

some problems here.

I have got a path that I want to replace and substitute using sed.
My command is this:
# echo /var/adm/messages | sed 's/[A-Za-z0-9]*\///g'
# messages

The returned results is "messages" which is what I want.

However, it will encountered problem if the path contains special chars like _, -, etc.
E.g. pathname is /hostname_home/mymessages

How can I specified in the command,
so that sed will be able to replace anything that is before the last "/" occurence?


Thanks in advance.
 
Old 11-20-2006, 09:57 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
I don't remember whether the '/' needs to be escaped with square brackets, but searching for the following should work:
Code:
sed -e 's/[^\/]*\///g'
Edit: The '*' makes the search string redundant. On my box, the following works fine:
Code:
$ echo /hostname_home/mymessages | sed -e 's/.*\///g'
mymessages
Couldn't you use basename instead? For example:
Code:
basename /hostname_home/mymessages

Last edited by gilead; 11-20-2006 at 10:01 PM.
 
Old 11-20-2006, 10:04 PM   #3
DriveMeCrazy
Member
 
Registered: Mar 2004
Posts: 70

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by gilead
I don't remember whether the '/' needs to be escaped with square brackets, but searching for the following should work:
Code:
sed -e 's/[^\/]*\///g'
Edit: The '*' makes the search string redundant. On my box, the following works fine:
Code:
$ echo /hostname_home/mymessages | sed -e 's/.*\///g'
mymessages
Couldn't you use basename instead? For example:
Code:
basename /hostname_home/mymessages

Thanks... ya think basename shld be easier.

Anyway just figured out another way...
sed 's/[A-Za-z0-9._-]*\///g'


 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using "sed" - a simple explanation needed midiox Linux - Newbie 2 04-03-2006 06:47 AM
Help on sed and perl needed. cash_05 Programming 7 09-03-2005 04:33 PM
sed regular expression help needed Dew Linux - Newbie 1 03-30-2005 02:59 PM
Sed/Awk command help needed. farmerjoe Programming 3 03-02-2005 11:13 AM
Sed Help Needed..Cutting out peice of file farmerjoe Programming 2 02-25-2005 06:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:10 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