Re: sed syntex in procmailrc question

Bart Schaefer <[email protected]>
Newsgroups gmane.mail.procmail
Message-ID <CABkvzcuR37mgLW-At2A-P1=2ZCHp9fcG7-ZLeGYgNhdu6-a0Fw@mail.gmail.com>
On Fri, Oct 14, 2011 at 5:45 PM, Harry Putnam <[email protected]> wrote:
> Bart Schaefer <[email protected]> writes:
>
>> To expand just a little on Harry's correct explanation:
>>
>> In the literal example you included, the single quotes around
>> 's/SUBJET/' are unnecessary.  You only need quotes to protect spaces,
>> dollar signs, and globbing characters like *?[].
>
> Again no expert here but let me move off sed for a moment since I'm
> more familiar with awk.  Aren't there cases with awk where spaces are
> not the issue but allowing a shell Variable to expand are what
> matters?

Yes.  That's why I also mentioned "dollar signs, and globbing characters."

> For example if you have a variable say, in the environment.  And you
> want it to expand inside some awk code in .procmailrc.   Wouldn't you
> need to do something like awk '/somestring/{print $0"'$MY_ENV_VAR'"}'

Yes, in that example (ignoring for now that $MY_ENV_VAR might expand
to something that breaks awk syntax) you do need to end the single
quotes before the environment variable reference and then resume
quoting again afterward.  In fact you may even mean:

awk '/somestring/{print $0"'"$MY_ENV_VAR"'"}'

Where there is one set of double quotes for awk (inside the shell
single quotes as in your original) and another set of double quotes
for the shell (outside but adjacent to the shell single quotes).
Unlike a lot of other programming languages, awk and (most) shells
automatically concatenate adjacent strings.  Double quotes in the
shell allow variable expansion but protect other special characters in
the expanded result.

(I inserted "(most)" because there are shells that use Perl or Tcl or
even Lisp syntax instead, but you'll probably never encounter one
unless you deliberately go looking.)

> I realize there are other constructs that would serve the same purpose
> and avoid quoting quagmires like:
>
>  awk -v var=$MY_ENV_VAR '{print var}'

Incidentally that would also fail (or at least not do what you expect)
in many shells in the event that $MY_ENV_VAR contains special
characters.  You really need

awk -v var="$MY_ENV_VAR" '{print var}'
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.