[ale] regex for basename? (was stripping filename extensions)

David S. Jackson deepbsd at earthlink.net
Sun Dec 1 12:16:42 EST 2002


On Sat, Nov 30, 2002 at 11:51:31PM -0500 Danny Cox <danscox at mindspring.com> wrote:
> Chris,
> 
> On Sat, 2002-11-30 at 15:45, Christopher Bergeron wrote:
> > Does anyone know how I can strip the extension from a filename in a bash 
> > script?
> > 
> 
> 	The "traditional" way is to use 'basename'.  It will also remove any
> preceding directory names.  But, if you include an extension to strip,
> it'll remove that too:
> 
> 	I=xyzzy.jpg
> 	echo $(basename "$I" .jpg)
> 
> will echo "xyzzy".
> 
> 	Of course, there are many ways to do what you want, as witnessed by the
> other suggestions.

Hi Danny et al,

I was wondering if there's a way to make basename accept a regex
for a suffix.  I just tried it, but no luck for me yet.  

I have a testdir (~/test) with one.jpg two.JPG three.jpg in it.
I tried basename $file {.jpg,.JPG}   (gives "too many arguments"
error).  I tried basename $file .jpg\|.JPG (ignores suffix
altogether; I guess it sees the expressions as a single string of
characters instead of two strings expanded from a regex).

The man and info pages don't seem to indicate a regex capability.

The only other way I can think to "trick" basename into handling
regex's is to do something like

for suf in .jpg .JPG; do
  ls *$suf | while read name; do
    basename $name $suf
  done
done

which seems like a lot of work when you could just go while read
file; do echo ${file%.*} and get the same result (for bash
and sh anyway).  

How was this handled in the olden days before the
${variable%.suffix} capability was added?  Just use sed or awk?

-- 
David S. Jackson                        dsj at dsj.net
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
You may already be a loser.
	-- Form letter received by Rodney Dangerfield.
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale






More information about the Ale mailing list