LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Create table with bash (https://www.linuxquestions.org/questions/programming-9/create-table-with-bash-4175585504/)

conejo_perez16 07-25-2016 08:26 PM

Create table with bash
 
hi all... how could I get a table that looks like this:

Variables:
one = 01 to 20 every 01
two = 12 to xx every 12 (this value keeps its increment of 12 every time the "one" variable increases by one in this case until the last value of "one" is reached)
three = 20 to 23 every 01

The table should look like this:
one two three
01 12 20
01 12 21
01 12 22
01 12 23
01 24 20
01 24 21
01 24 22
01 24 23
01 36 20
01 36 21
01 36 22
01 36 23
02 48 20
02 48 21
02 48 22
02 48 23
02 60 20
.. .. ..
20 xx 23

I think you can see the pattern!

I've been thinking over and over but no right answer came to my mind, I tried a triple for loop but could not get it to work.

Any hint would be appreciated!

Thanks!

notKlaatu 07-25-2016 08:37 PM

The "table" is easy; I'll assume you know how to print stuff to a shell.

For maths part, look into the `bc` command or else just variable incrementing.

You'll probably want to use nested `while` loops.

Hope that helps.

conejo_perez16 07-25-2016 11:03 PM

thanks, do you have any idea how to declare a variable resulted from arithmetic operatio inside an array variable?

for example

array=($var $var+12 $var+24 $var+36)

NevemTeve 07-25-2016 11:12 PM

You want to print this, or put it into a variable? If the former then it is three nested cycle and one printf

conejo_perez16 07-25-2016 11:16 PM

ok thanks i will try tomorrow morning and put the code here if i get stuck again at some point

grail 07-26-2016 12:11 AM

I would add that your explanation of the fields does not follow your example data!

If we ignore column 2 for the moment, even though columns 1 and 3 start with different numbers your incremental information was the same:

one = 01 to 20 every 01
three = 20 to 23 every 01

Yet your example shows:
Code:

one two three
01  12  20
01  12  21
01  12  22
01  12  23
01  24  20
01  24  21
01  24  22
01  24  23

Here we can clearly see column three increasing by 1 each time until the threshold of 23 is reached, yet column one has not increased at all for any of the 8 rows shown.

So either you need to provide a better explanation or your current example needs to be altered.

keefaz 07-26-2016 08:25 AM

Quote:

Originally Posted by conejo_perez16 (Post 5581245)
hi all... how could I get a table that looks like this:

Variables:
one = 01 to 20 every 01
two = 12 to 36 every 12 (this value keeps incrementin by 12 every time the "one"
variable increments
three = 20 to 23 every 01

column 2 has values greater than 36 in your example

conejo_perez16 07-26-2016 10:34 AM

Thanks for pointing out my mistake grail and keefaz, I just modified the original post, In fact the problem is that every time the variable "one" increments i want to keep the variable "two" increasing by 12 until the last value of "one" is reached

Im creating a little script, in a few minutes i will post it so you can see where I have the problem!

gda 07-26-2016 11:35 AM

If I understood correctly, this following bash script should generate the table as you requested:

Code:

#!/bin/bash

two=12
for one in $(seq 1 1 20)
do
        for three in $(seq 20 1 23)
        do
                printf "%02d\t%02d\t%02d\n" $one $two $three
        done
two=$((two+12))
done


conejo_perez16 07-26-2016 12:13 PM

Thanks a lot gda! it works!

michaelk 07-26-2016 12:27 PM

gda's code
Code:

01 12 20
01 12 21
01 12 22
01 12 23
02 24 20
02 24 21
02 24 22
02 24 23
03 36 20
03 36 21
03 36 22
03 36 23

Your example
Code:

01 12 20
01 12 21
01 12 22
01 12 23
01 24 20
01 24 21
01 24 22
01 24 23
01 36 20
01 36 21
01 36 22
01 36 23

Not the same. Can you post what you came up with yet?

keefaz 07-26-2016 12:53 PM

The example is still confusing, why the value 56 on column two?

column 1 is incremented by one after column 2 has incremented by 12 two times, correct?

michaelk 07-26-2016 12:57 PM

Think about how nested loops work. The only thing different about column 2 is that it always is incremented by 12.

gda 07-26-2016 01:09 PM

Quote:

Originally Posted by conejo_perez16
Variables:
one = 01 to 20 every 01
two = 12 to xx every 12 (this value keeps its increment of 12 every time the "one" variable increases by one in this case until the last value of "one" is reached)
three = 20 to 23 every 01

My code should be consistent with that!
Anyway you are right the example table is not consistent with these requirements. So I supposed the table was wrong. Is my interpretation correct?

michaelk 07-26-2016 01:22 PM

Yes, but without knowing the relationship between one,two and three there are many possibilities.

Only the OP knows...


All times are GMT -5. The time now is 04:07 PM.