Columns 4 and 5 are X and Y coordinates, column 6 is the elevation
I would like to add 2 new columns at the end of the file with values
the distance between first(X)(Y) and last location (X)(Y), based in 2 rows
the difference in elevation = ($6-prev6)
How to calculate the requested values and add to last columns, and when the records appears only 1 time(columns 1 and 2 single row ), write values 0.00 0.00
Input file
48047 41513 2 373512.79 2542085.84 154.53 12.64 90.63
48047 41513 3 373513.29 2542085.00 154.52 12.34 91.02
48047 41525 2 373663.51 2542087.65 153.93 12.36 90.70
48047 41525 3 373662.27 2542088.44 153.99 12.76 86.99
48049 39785 2 351912.93 2542112.25 160.91 12.16 90.61
48049 39797 2 352063.17 2542112.38 160.19 12.10 90.66
48049 39809 2 352213.12 2542113.01 159.96 12.24 90.75
Desired Output
48047 41513 2 373512.79 2542085.84 154.53 12.64 90.63
48047 41513 3 373513.29 2542085.00 154.52 12.34 91.02 0.98 0.01
48047 41525 2 373663.51 2542087.65 153.93 12.36 90.70
48047 41525 3 373662.27 2542088.44 153.99 12.76 86.99 1.47 0.06
48049 39785 2 351912.93 2542112.25 160.91 12.16 90.61 0.00 0.00
48049 39797 2 352063.17 2542112.38 160.19 12.10 90.66 0.00 0.00
48049 39809 2 352213.12 2542113.01 159.96 12.24 90.75 0.00 0.00
My code works only when the records in columns 1 and 2 are duplicate. If they are not duplicate my code delete the single lines.
Code I use to get the values
awk '{
pp[NR] = $2
ll[NR] = $1
x[NR] = $4
y[NR] = $5
z[NR] = $6
} END {
first = 1
count = 0
for ( i=1 ; i <= NR ; i++ ) {
if (pv != pv[i+1] || line != line[i+1]) {
last = i
count++
dx = x[first]-x[last]
dy = y[first]-y[last]
el = z[first]-z[last]
ele = sqrt(el^2)
len[count] = sqrt(dx^2+dy^2)
if ( len[count] <= 1000 && len[count] > 0 ) {
printf ("%8.2f %8.2f \n",
len[count],ele)
}\
first = i+1
}
}
}' file
Thanks in advance.