bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] Assigning RegExp Variables on the Command Line


From: arnold
Subject: Re: [bug-gawk] Assigning RegExp Variables on the Command Line
Date: Mon, 07 Oct 2019 02:58:40 -0600
User-agent: Heirloom mailx 12.5 7/5/10

Hello.

I think you're overreacting. The probabilities of such a value for a
shell variable are from low to zero.  If you really need something like that
you can use

        gawk -e "BEGIN { myvar = \"$shellvar\" }" ...

instead of -v.

I don't see a reason to add such warnings to the man page or to the manual.

Gawk user's are expected to know the language and in general
to know what they're doing.  If they don't, that's what the manual
is for.

Arnold

Stephane Chazelas <address@hidden> wrote:

> 2019-10-06 13:00:15 +0300, Marcus:
> [...]
> > This version works for me:
> > 
> > $ /path/to/gawk -v 're1=@/foo|bar/' 'BEGIN { print typeof(re1); print re1; 
> > exit }'
> > regexp
> > foo|bar
> [...]
>
> Woah, I had missed that new /feature/. I can't say I like much
> the fact that
>
> awk -v awkvar="$shellvar" '...'
>
> no longer works as expected when $shellvar starts with @/ and
> ends in /. I've just updated a few articles on
> unix.stackexchange.com to warn against that in addition to the
> problems with backslash.
>
> https://unix.stackexchange.com/questions/482036/can-i-use-a-variable-storing-a-regular-expression-wherever-a-regular-expression/482039#482039
> https://unix.stackexchange.com/questions/394953/output-every-n-column-from-input/394956#394956
> https://unix.stackexchange.com/questions/327317/escaping-backslash-with-awk-in-osx/327322#327322
> https://unix.stackexchange.com/questions/297134/using-bash-variable-with-escape-character-in-awk-to-extract-lines-from-file/297142#297142
> https://unix.stackexchange.com/questions/120788/pass-shell-variable-as-a-pattern-to-awk/120806#120806
> https://unix.stackexchange.com/questions/115224/print-line-after-nth-occurrence-of-a-match/115229#115229
> https://unix.stackexchange.com/questions/113796/do-i-need-to-encapsulate-awk-variables-in-quotes-in-order-to-sanitize-them/113799#113799
> https://unix.stackexchange.com/questions/87367/can-awk-use-field-identifiers-also-for-shell-strings-variables/87376#87376
> https://unix.stackexchange.com/questions/56128/external-variable-in-awk/56141#56141
> https://unix.stackexchange.com/questions/50044/use-a-shell-variable-in-awk/56190#56190
>
> It may be worth adding some warning in the manual (in both info
> gawk Assignment\ Options and info -- gawk -v) that
>
> awk -v awkvar="$shellvar" '...'
>
> and
>
> awk '...' awkvar="$shellvar"
>
> don't give the awkvar the same value as shellvar if $shellvar contains
> backslashes or starts with @/ and ends with /.
>
> How does one assign @// to a *string* variable with -v btw?
>
> awk -v 'var=\@//'
>
> seems to work but issues a warning.
>
> POSIXLY_CORRECT=1 awk -v var=@//
>
> seems to work but POSIXLY_CORRECT has a few other potentially
> undesirable side-effects (like honouring the locale for the
> decimal radix)
>
> I note that -F @/./ doesn't do the same as -v FS=@/./
>
> -- 
> Stephane
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]