zsh-users
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@candle.brasslantern.com>
To: zsh-users@sunsite.dk, Vincent Lefevre <vincent@vinc17.org>
Subject: Re: Parameter expansion: tr?
Date: Fri, 6 Apr 2001 16:53:53 +0000	[thread overview]
Message-ID: <1010406165353.ZM12264@candle.brasslantern.com> (raw)
In-Reply-To: <20010406151348.A27201@greux.loria.fr>

On Apr 6,  3:13pm, Vincent Lefevre wrote:
} Subject: Parameter expansion: tr?
}
} I've seen how one can do a string substitution in a parameter expansion,
} but how can I do a tr, e.g. to swap the "." and "/" characters?
} 
} Is there a simple way to do that, or do I need associative arrays?

The following requires 3.1.9 or later:

  function ztr {
    setopt extendedglob noshwordsplit
    local chunk=''
    while read -u0k 4096 chunk; do
	print -Rn ${chunk//(#b)([$1])/${2[${1[(I)$match]}]}}
	chunk=''
    done
    # "read -k" will exit nonzero on the last partial chunk, print it
    (( $#chunk )) && print -Rn ${chunk//(#b)([$1])/${2[${1[(I)$match]}]}}
  }

% ztr "abcde ." "NOPQR-/" <<EOF
The quick brown fox jumped over the lazy dog.
EOF
ThR-quiPk-Orown-fox-jumpRQ-ovRr-thR-lNzy-Qog/
% 

I think the "read -k" behavior on a partial chunk is a bug, but I seem to
recall some zsh-workers discussion to the effect that other shells also
behave that way.  I may be thinking of some other "read" behavior, though.

Using ${1[(I)$match]} emulates the real `tr aaa xyz` == `tr a z` behavior.
If you give the above function only one argument, it acts like "tr -d".
"tr -s" and "tr -c" left as exercises for the reader.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


      reply	other threads:[~2001-04-06 16:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-06 13:13 Vincent Lefevre
2001-04-06 16:53 ` Bart Schaefer [this message]

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=1010406165353.ZM12264@candle.brasslantern.com \
    --to=schaefer@candle.brasslantern.com \
    --cc=vincent@vinc17.org \
    --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).