How to change Cartesian file coordinates in an automated way

Asked

Viewed 113 times

2

I have a certain folder with several files, and these in turn contain inside description of coordinates points cartesian X and Y.

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

Well, what becomes the crucial point here, is to make the insertion coming from a printer/counter until end all the files contained in the folder.

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

list of 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 the 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 I got the logic right! The dots are being added by integer numbers 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 right after the second X of the second file for 200, and for the third file 300, in the fourth X 400 file and the fifth X 500 file.

Continue calculating/summing from 100 to 100 and adding in the next 5 files, and so it is done for every 5 file cycles. This is for the X axis, ja for the Y axis, it would be almost the same, however we have to take into account the value is lower, being of 5 in 5 considering that should set the same value for the first 5 files and make 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

inserir a descrição da imagem aqui

  • Mathematics is easy to do with shell, but I must admit that I was somewhat lost with the structuring and distribution of the files. Can you show an example of before and after? With the names of the desired files and such?

  • @Jeffersonquesado You may want to assemble this structure to get an idea on your own operating system that is using either MAC or LINUX. Do $ mkdir teste, and then create some blank files with the following command: $ for ARQ in {0..15}; do touch teste/$ARQ; done, then you will only have the job of opening them and adding the coordinates X 0 and Y 0 for everyone else, it will be the same. Now that I need it is elaboration of the script where you should add and increase and replace these zero(s).

1 answer

1


Let’s iterate over a number of desired coordinates. For that, I’ll need a little bit of integer arithmetic in the bash and, in iteration, let’s use a command expansion (I talk more about command expansions in this other answer).

Whole arithmetic

A basic arithmetic about integer numbers. This is something built-in in the bash, so I’m going to appropriate this math in the answer.

Answer quickly, how much (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 split operation over the real numbers. In the case of entire split, it is a binary operator over two integers resulting in another integer:

Divisão: operação binária sobre inteiros resultando em inteiros

So, considering the question now about whole arithmetic, what is the result of (x/5) * 5?

Put into words, the answer is:

The largest multiple of 5 less than or equal to x

It is also possible to write this differently, in a more algebraic way:

x - (x % 5)

Okay, interesting, but why is this important?

To do the calculations. I increment the value of Y every 5 iterations, at the ratio of 5 units. Look at the following pseudo-code:

x0 é fornecido como 100
y0 é fornecido como 0

x_inc é fornecido como 100
y_inc é fornecido como 5

y_step é fornecido como 5

para i no intervalo [0, 49]:
    x = x0 + i * x_inc
    y = y0 + (i/y_step) * y_inc

    operação com x e y

Where:

x0 is the initial value of x
y0 is the initial value of y
x_inc is how much x has the value incremented with each jump
y_inc is how much y has the value incremented with each jump
y_step is the number of iterations before there is an increment of y

Integer arithmetic expansion

In bash, to do the arithmetic expansion, you need to be in the arithmetic environment. It starts with $(( and ends with )) (the spaces in my tests were not necessary, but bash is a sacred language ;-) that needs to respect the spaces).

Run 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, not needing therefore the $ for its expansion forces.

Solution

I will use the above pseudo-code as the basis for creating the bash. Note that the shebang of bash is #!/bin/bash, nay #!/bin/sh. #!/bin/sh points to the default shell of the system, which can sometimes be itself Bourne Shell; bash is an evolution of this shell, the Bourne-Again Shell. I don’t like to wear #!/bin/sh, on several computers that I picked up at the beginning of the decade of 2010 still used the Bourne Shell and didn’t have the syntax I’d like to use from bash.

#!/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
  • @Diegohenrique, note that it is easier to create the file than to edit its contents (if the contents of the file is only the coordinate). Even so, you can do the subtituition if necessary using the sed, but recommend only in cases where there is more information in those files

  • @Diegohenrique, I just updated redirecting the output to be according to your

  • 1

    The original file does have more lines in its content. But for didactic purposes I put into question and comment fictitious files to get a sense of way isolated from the main purpose.

  • @Diegohenrique will upgrade to use sed as it should be, but I can’t now ;-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.