[SGVLUG] Shell fun: cutting the last field of a record...

John E. Kreznar jek at ininx.com
Fri Mar 24 15:30:20 PST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dustin Laurence <dustin at dogbert.laurences.net> writes:

> On Fri, Mar 24, 2006 at 01:23:05PM -0800, Emerson, Tom wrote:

> > >     $ echo $whatever | tr -d "\015"

> > That's two suggestions for "tr", so that looks like a winner

> My vote would be to *only* let through [a-zA-Z0-9_.] or something like
> that, to avoid some future surprise when you forgot to disallow some
> wierd character.

> Depending on exact needs you could also substitute '_' or 'X' for every
> disallowed character, to avoid accidental collisions.  That probably
> isn't needed for this particular app, but it's a thought.  Ideally
> you'd use a character you don't otherwise allow so there is no confusion
> between 'X\015' and 'XX'.

Yeah.  In fact autoconf has portable sed code to do this:

   # Sed expression to map a string onto a valid variable name.
   as_tr_sh="eval sed 'y%*+%pp%;s%[[^_$as_cr_alnum]]%_%g'"

where as_cr_alnum is defined earlier as effectively [a-zA-Z0-9].  See
/usr/share/autoconf/autoconf/autoconf.m4f on a Debian box with the
autoconf package installed.  This has an additional conversion of all
"*" and "+" to "p" which your application may not want.  (The double
square brackets pertain to m4 quoting.)

- -- 
 John E. Kreznar jek at ininx.com 9F1148454619A5F08550 705961A47CC541AFEF13
  ... who has not requested or consented to any political jurisdiction.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>

iD8DBQFEJIDbYaR8xUGv7xMRAojAAJ99pX/ceu1Tq06Q47SbNNBiI1pfPwCfYx3F
qYDaEVM8TWDpb8649+ETKBc=
=KKN9
-----END PGP SIGNATURE-----



More information about the SGVLUG mailing list