[GRASS5] r.to.pg: awk question

Andreas Lange Andreas.Lange at Rhein-Main.de
Tue Jul 25 15:12:00 EDT 2000


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 at geog.uni-hannover.de with
> subject 'unsubscribe grass5'

-- 
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