0

I have a 5 column file

2       649     2       82      1
3       651     1       83      1
2       652     3       84      2
...     ...     ...     ...     ...

The first column is n number of points in segment, the second is the x coordinate, the third is delta x, the delta between the current x coordinate and the next one, similarly the fourth column is the y coordinate and fifth is delta y. I need to generate all points in the segments so the output should be, from the data in the first line

649     82
650     82.5

From the data in the second line

651     83
651.33  83.33
651.67  83.67

From the data in the third line

652     84
653.5   85

Any idea How to do it?

1
  • You also need to explain how to arrive at that output from the input. Is it sorted? some other way? Commented Jun 8, 2014 at 20:11

1 Answer 1

2

This will do:

    awk '{n=$1; x=$2; dx=$3; y=$4; dy=$5;  \
    for(i=0;i<n;i++) printf "%.2f %.2f\n", x+i*dx/n, y+i*dy/n; }' file

You can adjust %.2f as you desired. For e.g. %.4f to print 4 digits of fraction.


I only used variables for clarity. Otherwise, you could simply do:

awk '{for(i=0;i<$1;i++) printf "%.2f %.2f\n", $2+i*$3/$1, $4+i*$5/$1; }' file
Sign up to request clarification or add additional context in comments.

7 Comments

Last question, I'd like to round the values so instead of printing 83.67 I'd like to print 84, how can I do it?
@e271p314 You mean ceil if fraction is more than .5 ? If so, you could do: awk '{for(i=0;i<$1;i++) printf "%d %d\n", $2+i*$3/$1+0.5, $4+i*$5/$1+0.5; }' file
Yes, ceil if more than .5 and floor otherwise
@e271p314 ceil is not an awk function. You could try this: awk '{for(i=0;i<$1;i++) printf "%d %d\n", $2+i*$3/$1+0.5, $4+i*$5/$1+0.5; }' file
There's no round or ceil function in awk. You could use this [round function[(gnu.org/software/gawk/manual/html_node/Round-Function.html) if you want. But I can't see why adding 0.5 won't work in all cases. Do you have any example where it'll fail?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.