Re: [dev] print utility

From: Sam Watkins <>
Date: Wed, 3 Apr 2013 16:25:24 +1100

> What is the shortest shell command you can write, that replaces $A with $B in a text stream for any A and B?

On Tue, Apr 02, 2013 at 06:32:19PM -0700, Noah Birnel wrote:
> A1="$(printf '%s' "$A" | sed 's,",\\",g; s,\\,\\\\,g')"
> B1="$(printf '%s' "$B" | sed 's,",\\",g; s,\\,\\\\,g')"
> awk '{ gsub("'"$A1"'", "'"$B1"'"); print }' textfile
> A little gross, maybe. So is the question.

My point is, it can be useful to have a tool which can do this basic
operation less grossly. Such as gres(1) / replace(1).

using awk this seems to work:

  awk '{ gsub(A, B); print; }' A="$A" B="$B"

Still a bit long, but much better than I can manage in sed or perl. Not sure
if that is standard awk or not, there's also a -v option to set values.

Received on Wed Apr 03 2013 - 07:25:24 CEST

This archive was generated by hypermail 2.3.0 : Wed Apr 03 2013 - 07:36:05 CEST