[GRASS5] r.to.pg: awk question

Hi again,

just now I submitted a script
r.to.pg
into CVS:
src/scripts/contrib/r.to.pg

It allows to write a GRASS raster map into a
one-column table into PostgreSQL. I use it to
analyse large datasets in "R" using the RPgSQL
library (R data proxy).

Following problem I have: Currently the table name
is static: "celltable". I would prefer the map's
name instead. But: As I use awk to generate the
INSERT statements, I have to get the table name
variable into awk. How to do this?

$1: GRASS raster map name

[...]

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", $TABLENAME, $1 >> "/tmp/pgimport.sql"}
END{}'

[...]

Above printf line is not working, I can just use a hard-coded
"tablename" instead of $TABLENAME. Means: How to use environmental
variables in awk?

Thanks for any advice!

Markus

PS: The cat text is not yet supported, but would be a good idea.

----------------------------------------
If you want to unsubscribe from GRASS Development Team mailing list write to:
minordomo@geog.uni-hannover.de with
subject 'unsubscribe grass5'

Hi Markus,

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.

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.

Example:
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", "'$1'", $1 >>
"/tmp/pgimport.sql"}
END{}' $TABLENAME

[...]

I did not test this, i personally would use perl or tcl to do that.

hth,

Andreas

Markus Neteler wrote:

Hi again,

just now I submitted a script
r.to.pg
into CVS:
src/scripts/contrib/r.to.pg

It allows to write a GRASS raster map into a
one-column table into PostgreSQL. I use it to
analyse large datasets in "R" using the RPgSQL
library (R data proxy).

Following problem I have: Currently the table name
is static: "celltable". I would prefer the map's
name instead. But: As I use awk to generate the
INSERT statements, I have to get the table name
variable into awk. How to do this?

$1: GRASS raster map name

[...]

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", $TABLENAME, $1 >> "/tmp/pgimport.sql"}
END{}'

[...]

Above printf line is not working, I can just use a hard-coded
"tablename" instead of $TABLENAME. Means: How to use environmental
variables in awk?

Thanks for any advice!

Markus

PS: The cat text is not yet supported, but would be a good idea.

----------------------------------------
If you want to unsubscribe from GRASS Development Team mailing list write to:
minordomo@geog.uni-hannover.de with
subject 'unsubscribe grass5'

--
Andreas Lange, 65187 Wiesbaden, Germany, Tel. +49 611 807850
Andreas.Lange@Rhein-Main.de - A.C.Lange@GMX.net

----------------------------------------
If you want to unsubscribe from GRASS Development Team mailing list write to:
minordomo@geog.uni-hannover.de with
subject 'unsubscribe grass5'

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. :slight_smile: )

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{}'

--
Professional Service around Free Software (intevation.net)
The FreeGIS Project (freegis.org)
Association for a Free Informational Infrastructure (ffii.org)

On Wed, Jul 26, 2000 at 10:58:40AM +0200, 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. :slight_smile: )

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{}'

Unfortunately my "awk" dislikes the ENVIRON.
Any other ideas? I would be glad to get this into GRASS 5b8.

I try -v var=val --assign=var=val
now.

Markus

----------------------------------------
If you want to unsubscribe from GRASS Development Team mailing list write to:
minordomo@geog.uni-hannover.de with
subject 'unsubscribe grass5'

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. :slight_smile: )

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@Rhein-Main.de - A.C.Lange@GMX.net

----------------------------------------
If you want to unsubscribe from GRASS Development Team mailing list write to:
minordomo@geog.uni-hannover.de with
subject 'unsubscribe grass5'