> 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.


Sam

Reply via email to