zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@sunsite.dk>
Subject: Re: Is this the Zsh way for doing this?
Date: Sun, 28 Mar 2004 00:55:31 +0000	[thread overview]
Message-ID: <1040328005531.ZM19873@candle.brasslantern.com> (raw)
In-Reply-To: <20040325223228.GA13221@DervishD>

On Mar 25, 11:35pm, DervishD wrote:
}
} tail +2 .CDinfo | while read song

This is fine, but you might prefer the more obvious:

tail +2 .CDinfo | while read ignored_word track_number track_name
do
    # At this point ignored_word is always "Track" and track_number
    # still has a trailing colon that we need to strip off.  Also
    # track_name contains single quotes, but based on this example:
    #    Track 10: 'This song really doesn't exist...'
    # they aren't balanced single quotes, so we have to strip them.
    # If the zero-padding gets done here too, it's clearer later.
    track_number=${(l.2..0.)${track_number%:}}
    track_name=${${track_name%\'}#\'}

    # At this point you might also want to clean wildcards and slashes
    # out of $track_name, but that's up to you.  Your original doesn't.

    # Now the "mv" command is quite obvious.
    mv audio_${track_number}.cdr "${track_number}.${track_name}.cdr"
done

If you really desperately want to write it all as one command rather
than have the two extra assignments:

    mv  audio_${track_number/

As to your specific pattern-matching questions:

}     ${song/#(#b)Track #([0-9]##):'(*)'/${(l.2..0.)match[1]}.${match[2]}}.cdr
} 
} AFAIK, when pattern matching takes place
} at the point of the "'(*)'", it is implicitly anchored to the end of
} the string and is the longest match

No, it's not implicitly anchored at the end, but it is the longest match,
which (since there is always a single-quote at the end of the string) is
equivalent in this case.

However, you do need to escape the single quotes, and there is a space
after the colon:

	(#b)Track #([0-9]##): \'(*)\'

} [Why can't I] use the (#b) flag before the '/' (together with 'song')
} or before the '#' (which indicates that 'Track' must match at the
} beginning of the string) [...?]

What you've asked is very similar to asking about C,

    Why can't I put the "--" before the "+" or before the "=" in
    the expression "song += --pattern" ?

This is because the / and the # together constitute an operator on the
value of the variable, whereas (#b) is part of the pattern which is an
argument to that operator.

} BTW, is that '#' necessary or not?

It's not necessary as long as none of the names is "The Track 27: song"
or some such.

} Do I need to specify too the end of the string anchor?

No.


  reply	other threads:[~2004-03-28  0:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-25 22:32 DervishD
2004-03-28  0:55 ` Bart Schaefer [this message]
2004-03-28  1:03   ` Bart Schaefer
2004-03-28 20:07     ` DervishD
2004-03-28 21:00       ` Bart Schaefer
2004-03-29 14:08         ` DervishD

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=1040328005531.ZM19873@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --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).