.sh and sed

Aiza aiza21 at comclark.com
Wed Jun 16 13:44:22 UTC 2010


Matthew Seaman wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> On 16/06/2010 12:16:06, Aiza wrote:
>> Trying to use sed to remove the path from the file name.
>> Variable has complete path plus the file name
>> /usr/local/etc/filename
>> Need variable containing only the file name.
>> Is the sed utility the best thing to use?
>> Is there some other utility better suited for this task.
>> How would sed by coded to do this?
> 
> sh(1) can do this alone, without recourse to any external programs.
> 
> path='/usr/local/etc/filename'
> fname=${path##*/}
> echo $fname
> 
> There is also an external program basename(1)
> 
> The same trick with sed(1):
> 
> fname=$( echo $path | sed -e 's,^.*/,,' )
> 
> but the built-in prefix matching stuff is preferable since it is more
> efficient.
> 
> 	Cheers,
> 
> 	Matthew
> 
> - -- 

Thanks for your help.

The fname=${path##*/} solution worked for.


More information about the freebsd-questions mailing list