[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Is there a way to assign to a hash via the -v option
From: |
Isabella Parakiss |
Subject: |
Re: [bug-gawk] Is there a way to assign to a hash via the -v option |
Date: |
Mon, 7 Mar 2016 17:07:38 +0100 |
User-agent: |
Mutt/1.5.24 (2015-08-30) |
On Mon, Mar 07, 2016 at 08:40:19AM -0700, address@hidden wrote:
> Peng Yu <address@hidden> wrote:
>
> > > David's suggestion is one option. Another is simply to mix the -e and -f
> > > command line options and do the assignment in a BEGIN rule suppplied
> > > on the command line.
> >
> > Can you provide an example for the above one?
>
> Here you go, shell quoting tricks included:
>
> $ cat test.awk
> BEGIN { print x[1], x[2] }
>
> $ x1="this is x1" x2="this is x2"
>
> $ cat test.awk
> BEGIN { print x[1], x[2] }
>
> $ gawk -e 'BEGIN { x[1] = "'"$x1"'" ; x[2] = "'"$x2"'" }' -f test.awk
> this is x1 this is x2
>
>
> Arnold
>
That's a bad, unsafe quoting example.
Better use something like this:
$ x1="this is x1" x2="this is x2"
$ export x1 x2
$ gawk -e 'BEGIN { x[1] = ENVIRON["x1"]; x[2] = ENVIRON["x2"] }' -f ...
---
xoxo iza