Rearranging Data Set

Hello everybody,

I've got the following problem:

The data set I have is an ASCII file containing a header over 4 lines and the actual data comprised of dezimal numbers in a 1000x1000 grid (1000 lines and 1000 columns).

Since I want to plot the data in GMT I need to convert it into the following format:

[x coordinate] [y coordinate] [value]

This means every single value will be in a single line preceeded by its coordinates. For simplicity lets say the upper left corner has coordinats 0,0 and the grid spacing is 500 in both directions.

I tried the following using "awk"

awk 'BEGIN {
xUL=0
yUL=0
for (i=1; i<=1000; i++)
      print "xUL+($i-1)*500", yUL+($i-1)*500, $i
}'

input.txt > output.txt

Since I'm posting my problem here it is obvious the the code above is not working. Can anybody tell why it is not working or a better solution?

Cheers
Evil

PS: I'm scripting in bash.

You are running a loop from 1 to 1000 over columns which don't exist and lines haven't been read. BEGIN takes place before any data is read.

Further, awk does not process expressions inside strings.

That you have a loop from 1 to 1000 doesn't tell awk to read 1000 lines anyway. awk has its own built in loop, which it will use if you don't specifically force it not to (by using BEGIN or END).

I think you want something like this:

awk '{ for(N=1; N<=NF; N++) printf("%d %d %s\n", N, NR, $N); }' inputfile > outputfile

NF is the number of columns for the current line.

1 Like

Welcome to the forum.

It is always beneficial to post sample input and output data, describe the underlying structure, show the (intended!) logics or algorithms that connect the two, and - in case of failures - what and where fails, and error messages.

In your case - which are the coordinates, and which are the values to plot? Is it [row No.] [col No.] field value ? When looping across the fields, does it make sense to use $(i-1) and $i , and then only increment by 1 (which would mean you're using $i twice)?

Thanks Corona688!

This is exactly what I needed! I was able to adjust your suggested Code to my needs and everything is working as intended. You guys are really fast.

Cheers
Evil

1 Like