zsh-users
 help / color / mirror / code / Atom feed
From: Andy Spiegl <zsh.Andy@spiegl.de>
To: ZSH User List <zsh-users@sunsite.dk>
Subject: parameter expansion or regex question
Date: Thu, 17 Feb 2005 04:03:55 +0100	[thread overview]
Message-ID: <20050217030355.GA2943@spiegl.de> (raw)

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)


             reply	other threads:[~2005-02-17  3:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-17  3:03 Andy Spiegl [this message]
2005-02-17  5:41 ` Bart Schaefer
2005-02-18  0:15   ` Andy Spiegl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050217030355.GA2943@spiegl.de \
    --to=zsh.andy@spiegl.de \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).