My input is a tab-separated text file with lat long in D-M-S. I require output to be in decimal degrees I have code in php, but this is very slow to calculate. Can this be done quicker using awk?
node name id latitude longitude seq
nodex name1 70 N53-24-31.126 W6-20-46.982 59126
nodex name2 173 N53-20-28.885 W6-14-52.400 16190X
nodex name3 173 N53-20-28.885 W6-14-52.400 16191T
My PHP code with Formula:
if ($dirLat == 'N') {$signLat = '+';} Else {$signLat = '-';}
if ($dirLat == 'E') {$signLon = '+';} Else {$signLon = '-';}
$latitudeDecimalDeg = $signLat . ($degLat + ($minLat/60) + ($secLat/3600));
$longitudeDecimalDeg = $signLon . ($degLon + ($minLon/60) + ($secLon/3600));
I really don't know if it'll be faster or not, but IMHO here's the fastest way you can do it in awk and preserve spacing:
I'd be interested to know if it is faster than your PHP script or not so if you get a chance to compare them, please post the results.
If you don't care about preserving spacing you can just use:
I am pretty certain that awk would be quicker. This is fairly easily accomplished with awk, split each field with
substr
andsplit
. I turned it into a function for easier reuse:If you have put the above into dms2deg and the data is in infile, you would use awk like this:
Output:
Or if you wanted to replace the existing fields:
Output:
Note that this does not preserve white-space, however
column
would take care of that:Output: