Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

parameter expansion or regex question



In order to learn something new I decided to write a zsh script instead of
a perl script and now I'm stuck. :-(

The task is the following: I've got a filename consisting of any number of
words separated by dots, e.g.
 file.one.as.an.example
 another.file.txt
 just.one.more.to.show.off

This filename may (or may not) contain one of the strings
 q1 q2 q3 q4 q5
also separated by dots.  More examples:
 file.one.q4.as.an.example
 q3.another.file.txt
 just.one.more.to.show.q1.off

All I need now is the filename _without_ this "q<1-5>" string.
The problem is that this string is normally surrounded by dots
but not if it's at the beginning or end of the filename.
One hour ago I thought that I finally solved it (see code snippet below)
but then I found that it doesn't work if it e.g. contains the word "freq1."
or ".q3po.".  Oh, boy - it sounded so simple at first!

Well, here is my current attempt:
 (input: FILENAME, output: CLEANFILENAME)

post=${FILENAME#*q<1-5>.}
if [ x"$post" = x"$FILENAME" ]; then
  # no string found -> nothing to do
  CLEANFILENAME=$FILENAME
else
  pre=${FILENAME%q<1-5>.$post}
  # now pre is empty (q?.foobar.jpg) or terminates with a . (foo.q?.bar.jpg)
  if [ ! -n "$pre" ]; then
    CLEANFILENAME=$post
  elif [ ${pre%.} = $pre ]; then
    # no string found -> nothing to do
    CLEANFILENAME=$FILENAME
  else
    CLEANFILENAME=$pre$post
  fi
fi

In Perl I'd do it similar to this:
 FILENAME =~ /(^|.)q[1-5](.|$)/;
But I have no idea how/whether zsh can handle regexps.

Uffda, it's already way to late to think so I better go to bed now.
Hope to hear from you guys tomorrow.  Thanks,
 Andy.

-- 
                              o      _     _         _
  ------- __o       __o      /\_   _ \\o  (_)\__/o  (_)          -o)
  ----- _`\<,_    _`\<,_    _>(_) (_)/<_    \_| \   _|/' \/       /\\
  ---- (_)/ (_)  (_)/ (_)  (_)        (_)   (_)    (_)'  _\o_    _\_v
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Whenever I watch TV and see those poor starving kids all over the world,
 I can't help but cry. I mean I'd love to be skinny like that, but not with
 all those flies and death and stuff.     (Mariah Carey)



Messages sorted by: Reverse Date, Date, Thread, Author