Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-05-2012
Registered User
 
Join Date: Oct 2008
Posts: 262
Thanks: 19
Thanked 6 Times in 6 Posts
Why does this SED example work?


Code:
$ x="/home/guru/temp/f1.txt"
$ echo $x | sed 's^.*/^^'

This will give the absolute path f1.txt. I don't understand WHY it works. How is it determining the last "/" character exactly?
Sponsored Links
    #2  
Old 07-05-2012
methyl methyl is offline Forum Staff  
Moderator
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
After 231 posts, you must realise that there is some variation in unix/Linux/Shell.
Please post what Operating System and version your are running and what Shell you use.

I would advise all other posters to not respond until the O/P provides proper context.
Sponsored Links
    #3  
Old 07-05-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,372
Thanks: 87
Thanked 478 Times in 458 Posts
Post withdrawn...

Last edited by elixir_sinari; 07-05-2012 at 05:25 PM..
    #4  
Old 07-05-2012
Registered User
 
Join Date: Oct 2008
Posts: 262
Thanks: 19
Thanked 6 Times in 6 Posts
BSD Sed, OS X 10.6.8 (Mac). Sorry.

Last edited by methyl; 07-05-2012 at 06:32 PM..
Sponsored Links
    #5  
Old 07-05-2012
methyl methyl is offline Forum Staff  
Moderator
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
This is going to need a sed expert. Anybody out there?

Personally I would use the unix basename command and be sure how it worked. Others may differ.
Sponsored Links
    #6  
Old 07-05-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,350
Thanks: 144
Thanked 1,756 Times in 1,593 Posts
It takes advantage of the greedy matching characteristic of sed, which means that it will try to find the longest match possible.. So in this case the longest match to .*/ is /home/guru/temp/ , so it will replace that with nothing, since the replacement part contains nothing.

The ^ plays no role, here other than that it is the character that delimits the match part and the replacement part. Normally forward slashes (/) are used for this but it can be almost any character. So instead, one could have used sed 's|.*/||' or s'=.*==' , for example.

--
If your scope is a single variable, than a more effective method is to use basename , as methyl suggests or to use variable expansion:

Code:
echo "${x##*/}"

The Following User Says Thank You to Scrutinizer For This Useful Post:
glev2005 (07-05-2012)
Sponsored Links
    #7  
Old 07-05-2012
Registered User
 
Join Date: Oct 2008
Posts: 262
Thanks: 19
Thanked 6 Times in 6 Posts
Greedy matching, perfect. Is this characteristic of all Sed versions? How do you determine this regex behavior of a particular command, such as Sed or any other?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
cant get this to work iago UNIX for Dummies Questions & Answers 6 09-06-2007 04:41 PM
why awk does not work here? fedora Shell Programming and Scripting 2 09-05-2007 10:51 PM
how does this work.... bishweshwar UNIX for Advanced & Expert Users 1 02-20-2007 05:15 AM
Script doesn't work, but commands inside work cheongww UNIX for Dummies Questions & Answers 2 11-14-2006 09:52 PM



All times are GMT -4. The time now is 10:31 PM.