|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Post withdrawn...
![]() Last edited by elixir_sinari; 07-05-2012 at 05:25 PM.. |
|
#4
|
|||
|
|||
|
BSD Sed, OS X 10.6.8 (Mac). Sorry.
Last edited by methyl; 07-05-2012 at 06:32 PM.. |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|