cut file extension

Joachim Schrod jschrod at acm.org
Wed Feb 20 16:04:52 UTC 2008


Owen Townend wrote:
> 
>      >>>   Look into `sed`. Something like this should do it
>      >>>   `sed -e 's/.*$//'`
>      >>>
>      >>>   That's from the top of my head though, check it first!
>      >> man basename may be useful :-)
>      >>
>      >> and the Owen's sed command isn't quite right, use 's/\..*$//'.
>      >
>      > ahah, that's closer to what I was aiming for, but it still doesn't
>      > account for files like 'file.name.ext' cutting them to 'file' though  it should only
>     match the end
>      > of the line... ?
> 
> I was thinking there must be a way to do this quickly in sed, 
> but I can't seem to find the right regex to match it correctly.
> ah well, a solution in search of a problem now with it fixed.

This is for the case that somebody needs this for arbitrary Unix 
systems, where bash might not be available. I.e., plain old sh 
programming... :-)

It would still not work, you need to supply the file name on stdin.
basename=`echo "$fname" | sed 's/\.[^.]*$//'` would do the trick.

Of course, one could also use
basename=`expr "$fname" : '.*\.\([^.]*\)$'`
(Please note the quote to support filename with spaces.)

And one should mention that basename, which was named as a 
solution, allows only to strip known suffixes, not arbitrary ones.

Best,
	Joachim

-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Joachim Schrod				Email: jschrod at acm.org
Roedermark, Germany





More information about the ubuntu-users mailing list