How to change the Cartesian coordinates of files, in an automated way

2

I have a certain folder with several files, and these in turn, contains in its interior description of Cartesian X and Y coordinate points.

However, I want to replace these points automatically and dynamically, ie use while to run all those files by running sed to change the X and Y axis values of the files.

Well, what becomes the crucial point here is to do the insertion coming from an adder / counter until all the files contained in the folder have finished.

The detail is that, for every 5 (five) files, the Y must receive identical values, and the next 5 files, a new number is added giving a jump from 0 - 5. See example:

list of the first 5 files:

X: 100   Y: 0

X: 200   Y: 0

X: 300   Y: 0

X: 400   Y: 0

X: 500   Y: 0

list of next 5 files:

X: 600   Y: 5

X: 700   Y: 5

X: 800   Y: 5

X: 900   Y: 5

X: 1000   Y: 5

list of the following 5 files:

X: 1100   Y: 10

X: 1200   Y: 10

X: 1300   Y: 10

X: 1400   Y: 10

X: 1500   Y: 10

I think you have already understood the logic all right! The points are being summed by integers to a counter, which puts them in the measure of an input of 5 files at a time, starting with the number 100 for the X axis, and soon after the second X of the second file to 200, and for the third file 300, fourth file X400, and fifth file with X500 axis.

Continue by calculating / adding 100 in 100 and adding 5 files in the next, and this is done for every 5 cycles of files. This is for the X axis, and for the Y, it would be almost the same thing, however we must take into account the value is smaller, being 5 in 5 considering that it should set the same value for the first 5 files and do it again the sum for the next 5 files and etc ...

Example

#!/bin/sh
X=100;
Y=0;
while [ $Y -lt 5 ]; do
   echo -e "X: $X\r";
   echo -e "Y: $Y\n";
   let X=$X+1;
   let Y=$Y+1;
done

Follow the result after running the script

    
asked by anonymous 18.06.2017 / 15:08

1 answer

1

Let's iterate over a number of desired coordinates. For this, I'm going to need some integer arithmetic in bash and, in iteration, we'll use a command expansion (I'll tell you more about command expansions in this other answer ).

Entire arithmetic

A basic arithmetic about integers. This is something built-in in bash , so I'll take that math in the answer.

Respond fast, how much is (x/5) * 5 ? If you thought it was x , you're still in the real numbers, and you'd be right if the question was about the operation divide over the real numbers. In the case of integer division, it is a binary operator on two integers resulting in another integer:

So,consideringthequestionnowaboutintegerarithmetic,whatistheresultof(x/5)*5?

Puttingitinwords,theansweris:

  

Thelargestmultipleof5islessthanorequaltox

Itisalsopossibletowritethisinamorealgebraicway:

  

x-(x%5)

Ok,interesting,butwhyisthisimportant?

Todothecalculations.IincrementthevalueofYevery5iterations,intheratioof5units.Lookatthefollowingpseudo-code:

x0éfornecidocomo100y0éfornecidocomo0x_incéfornecidocomo100y_incéfornecidocomo5y_stepéfornecidocomo5parainointervalo[0,49]:x=x0+i*x_incy=y0+(i/y_step)*y_incoperaçãocomxey

Where:

  x0istheinitialvalueofx
y0istheinitialvalueofy
x_incishowmuchxhastheincrementedvalueateachjump
y_incishowmanyyhastheincrementedvalueateachjump
y_stepisthenumberofiterationsbeforethereisanincreaseofy

Entirearithmeticexpansion

Inbash,todothearithmeticexpansion,youneedtobeinthearithmeticenvironment.Itstartswith$((andendswith))(thespacesinmytestswerenotneeded,but bash is a sacred language ;-) you need to respect the spaces).

Rotate this as an example:

echo $(( 13/5 ))
x=49
echo $(( (x/5) * 5 ))
echo $(( x - (x%5) ))

In the arithmetic environment the variables are interpreted, thus not needing $ to force their expansion.

Solution

I will use the above pseudo-code as the basis for creating the script in bash . Note that the shebang of bash is #!/bin/bash , not #!/bin/sh . #!/bin/sh points to the default shell of the system, which can sometimes be the Bourne Shell itself ; bash is an evolution of this shell, the Bourne-Again Shell . I do not like using #!/bin/sh , on several computers I picked up in the early 2010s I still used the Bourne Shell and did not have the syntax I'd like to use from bash in>.

#!/bin/bash

x0=100
y0=0

# incrementos
x_inc=100
y_inc=5

y_step=5

# quantidade de coordenadas desejadas
n_coords=50

for i in 'seq 0 $(( n_coords - 1 ))'; do
    x=$(( x0 + i*x_inc ))
    y=$(( y0 + (i/y_step)*y_inc ))

    echo "X:$x Y:$y" > teste/$i
done
    
19.06.2017 / 15:24