[GRASS5] r.to.pg: awk question
Andreas Lange
Andreas.Lange at Rhein-Main.de
Wed Jul 26 07:22:23 EDT 2000
Hi,
revisting my pocket reference on awk shows:
ENVIRON["VAR"] only works if you export the variable from the shell
script as follows:
export VAR="somestring"
echo "Test
Test
Test" | awk '{printf "%s: %s\n", ENVIRON["VAR"], $1}'
Not documented, but works for me:
VAR="somestring"
echo "Test
Test
Test" | awk '{printf "%s: %s\n", "'$VAR'", $1}'
hope that clarifies this.
Andreas
Bernhard Reiter wrote:
>
> On Tue, Jul 25, 2000 at 09:12:00PM +0200, Andreas Lange wrote:
> > the awk prog enclosed in '' is not evaluated by the shell so that there
> > is no substitution of variable names. This would be bad as all other
> > strings would be evaluated by the shell.
>
> Correct.
>
> > You could echo the program to a temporary file and use awk -f tmpfile.
> > Or you can supply the string on the awk command line and use "'$1'" to
> > get it in the awk skript.
>
> Both too complicated.
> gawk (which is the standard awk now, derived from the POSIX version of
> the new awk) has the ENVIRON field. So you can access environment
> variables from within awk. Try awk ' { print ENVIRON["PATH"] } '
> (and press return, because awk still reads lines. :) )
>
> The second possibility is to use the "-v" flags to bring variables in.
>
> > > TABLENAME="somestring"
> > > #get the raster values and write out SQL:
> > > r.stats -1 nv='-9999' in=$1 | awk 'BEGIN{}
> > > NR == 1{}
> > > {printf "INSERT INTO %s VALUES ( %.5f);\n", ENVIRON["TABLENAME"] , $1 >> "/tmp/pgimport.sql"}
> > > END{}'
>
--
Andreas Lange, 65187 Wiesbaden, Germany, Tel. +49 611 807850
Andreas.Lange at Rhein-Main.de - A.C.Lange at GMX.net
----------------------------------------
If you want to unsubscribe from GRASS Development Team mailing list write to:
minordomo at geog.uni-hannover.de with
subject 'unsubscribe grass5'
More information about the grass-dev
mailing list