cut file extension

David Restall - System Administrator dave at restall.net
Wed Feb 20 14:45:24 UTC 2008


Hi,

A bit of snipping is probably needed here :-)

>Owen Townend wrote:
>> On 2/20/08, David Restall - System Administrator <dave at restall.net> wrote:
>>> Hi,
>>>
>>>> On 2/20/08, Germain Morbe <germain.morbe at web.de> wrote:
>>>>> Hi all,
>>>>>
>>>>> im looking for a solution to strip the file extension
>>>>> within a bash script.
>>>>>
>>>>> thanks

Snip

>>>> Hey,
>>>>   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/\..*$//'.
>>>
>>> The cut command could also be used using a delimter of . and so could
>>> awk.  TMTOWTDI :-)
>>>
>>> TTFN
>> 
>> 
>> 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... ?
>> 
>$ file=file.name.ext
>$ echo ${file%.*}
>file.name
>
>Is that what you want? :-)

Sed still works if you do :-

echo file.part2.part3..name.ext | sed 's/\.[^.]*$//'

but Heinrich has provided a far more elegant solution.  It's no good
if you're not using bash though :-)

If you regularly encounter this kind of problem, have a look for
regular expressions, once you have used them you will wonder how you got
by without them :-)

TTFN



D
ubuntu/users-2008-02-20.2.tx                   rebehn at ant.uni-bremen.de
                                               germain.morbe at web.de
                                               owen.townend at gmail.com
                                               ubuntu-users
+----------------------------------------------------------------------------+
| Dave Restall, Computer Nerd, Cyclist, Radio Amateur G4FCU, Bodger          |
| Mob +44 (0) 7973 831245      Skype: dave.restall             Radio: G4FCU  |
| email : dave at restall.net                     Web : Not Ready Yet :-(       |
+----------------------------------------------------------------------------+
| BOFH excuse #436:                                                          |
|                                                                            |
| Daemon escaped from pentagram                                              |
+----------------------------------------------------------------------------+





More information about the ubuntu-users mailing list