[GRASS-user] Reading file with with space separators

Hamish hamish_b at yahoo.com
Wed Apr 17 04:25:20 PDT 2013

Ralf wrote:
> > I have xyz files with the format
> > ..x..y...value
> > where <.> stands for white space. The spacing is
> > two spaces between x and y and three spaces to data column.

Markus Metz wrote:
> [ rather difficult to produce a file with different kinds of
> spacing between columns ]

fwiw it's pretty standard for fortran output to come with
fixed column widths.

echo "1 2 3" | awk '{printf("%10.3f%10.3f%10.3f\n"), $1, $2, $3}'

> You could try
> cat dgm10_32292_5548_2_rp.xyz | tr -s ' '
>  >dgm10_32292_5548_2_rp_one_space.xyz
> r.in.xyz fs='space', in=dgm10_32292_5548_2_rp_one_space.xyz
>  out=test

Yes, I think using 'tr -s' is the best way.

(you can pipe the result directly into r.in.xyz too if you like)

> > I used
> > r.in.xyz fs='space', in=dgm10_32292_5548_2_rp.xyz out=test
> > and obtain:
> > ERROR: Bad y-coordinate line 1 column 2. <>

if it really is _always_ two,three spaces between columns,
you could set x=3 y=5 z=8. But maybe a .0 is left off somewhere
and the number of spaces between columns changes?

> > In R it is no problem - the simple read.table with
> > sep="" for any whitespace works. And with rasterFromXYZ
> > (raster package) I can create a raster without problems.

r.in.xyz (and most GRASS modules) split by each literal sep= char,
not until it finds another number the way glibc funtions or awk
might. An offshoot of this (mostly for importing .csv tables
from spreadsheets with v.in.ascii) is that empty data columns
are supported as containing NULL, and not messing up the placement
of columns to the right.


More information about the grass-user mailing list