Wednesday, May 6, 2009

Editing coordinates with gawk

If you don't have it yet, download Gawk from here:
http://gnuwin32.sourceforge.net/packages/gawk.htm

After install, you can start gawk from the 'bin' folder.
(Or you may edit the Path environment variable, to access gawk from everywhere.)

Create a new file, named for instance coord.awk

Let us assume we have a file full of coordinates named coord.txt, and it is formatted something like this:
-104097730 256262806 974997
-104100494 256261484 974564
-104103491 256260058 974112
...

Put this little program into coord.awk:

BEGIN{
print "coord.x, cooord.y, coord.z";
};

{
print $1 ", " $2 ", " $3;
}


(Small explanation: the first block simply prints a header, before doing anything.
The second block will run for every row, printing the first three strings in that row, separated by commas. In our case, these strings will be the x, y and z coordinates.)
Now we can make this work with this command:

gawk -f coord.awk coord.txt

(-f tells the awk program is in a file (coord.awk in this case), and we want to run it on coord.txt)
After running this, it will put the result on the screen.
If we would like to store this stuff in a file, we could write:

gawk -f coord.awk coord.txt > newcoord.txt

After this, we'll have the comma separated coordinates with a simple header in a new file.

coord.x, cooord.y, coord.z
-104097730, 256262806, 974997
-104100494, 256261484, 974564
-104103491, 256260058, 974112
...

If that is what you need... but this little program is easy to modify, so you can use it to edit huge files the way you want, creating headers, reformat or edit the coordinates in each row separately, and maybe putting a footer with an 'END' block.)