cut file extension

Owen Townend owen.townend at gmail.com
Wed Feb 20 23:28:22 UTC 2008


On 2/21/08, David Restall - System Administrator <dave at restall.net> wrote:
>
> 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                                              |
>
> +----------------------------------------------------------------------------+
>
>
Hey,
  Thanks, that regex makes sense (20-20 hindsight) turns out it was sed that
I
was confused about. Thanks for the clarity.

cheers,
Owen.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20080221/bd02a16e/attachment.html>


More information about the ubuntu-users mailing list