Re: FAQ question

From: Ed Morton (morton_at_lsupcaemnt.com)
Date: 04/27/05


Date: Wed, 27 Apr 2005 11:12:33 -0500


Bruce Barnett wrote:
> Ed Morton <morton@lsupcaemnt.com> writes:
>
>
>> g) awk '$0 == '"$svar"'' file
>
>
> I think it's worth highlighting this technique, because it works for
> any utility with any shell.

But it's evil and must be stopped ;-). Seriously, though, even though it
may be necessary for other utilities it is way down in the list of ways
to pass variables to awk since every newer awk provides better alternatives.

  I usually tell people about this technique
> first, because of this. If you need to pass a variable into sed, for
> instance, this is the technique I usually use.

In 25 years of using sed I've never done that. I just use double quotes
around the whole script:

PS1> echo "a${HOME}b" | sed 's?'"$HOME"'?p?'
apb
PS1> echo "a${HOME}b" | sed "s?$HOME?p?"
apb

since it's shorter to type and easier to read.

> Perhaps you can have a section that discusses passing variables into a
> generic utility, and then have the awk section mention the previous
> options, and then give those options that only AWK supports.

I don't want to do that because:

a) I don't know enough about that topic to write a FAQ on it.
b) I've never seen that question raised so it'd probably be
inappropriate for a FAQ.
c) I'm just trying to straighten the record on how to pass variables to
awk specifically since it is a FAQ in several NGs and there's no
accurate answer anywhere I can find today.
d) My answer is already much longer than I'd have liked.

        Ed.



Relevant Pages

  • Re: Dynamic loading of javascript files into web pages
    ... I saw a video online. ... Why wasn't it in the FAQ? ... Video presentations often contain advice that is either ... I don't recommend this technique either. ...
    (comp.lang.javascript)
  • Re: c.u.s. FAQ, take two
    ... you can do the operation with awk without ... >> This technique works even in a pipe or when otherwise reading stdin. ... the method that reads the file twice ... Granted, this will make toe FAQ a bit longer, but I ...
    (comp.unix.shell)
  • Re: c.u.s. FAQ, take two
    ... >> This technique works even in a pipe or when otherwise reading stdin. ... just moderate programming experience and some familiarity ... with basic awk syntax and shell quoting rules should be enough. ... How's that as a candidate for FAQ? ...
    (comp.unix.shell)
  • Re: Help Bleeding Brakes
    ... Using the patented Mavis Beacon "Hunt&Peck" Technique, Paul Edwards typed ... >What I should have done was read the FAQ. ...
    (uk.rec.motorcycles)
  • Re: awk variables not passing to redirection issue
    ... > I wrote the comp.unix.shell FAQ on passing shell variables to awk and ... > like to update the FAQ. ... *dissipate flashback sequence* ...
    (comp.lang.awk)