LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2016, 08:26 PM   #1
conejo_perez16
LQ Newbie
 
Registered: Aug 2012
Posts: 11

Rep: Reputation: Disabled
Question 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!

Last edited by conejo_perez16; 07-26-2016 at 04:34 PM.
 
Old 07-25-2016, 08:37 PM   #2
notKlaatu
Senior Member
 
Registered: Sep 2010
Location: Lawrence, New Zealand
Distribution: Slackware
Posts: 1,077

Rep: Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732
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.
 
Old 07-25-2016, 11:03 PM   #3
conejo_perez16
LQ Newbie
 
Registered: Aug 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
Question

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)
 
Old 07-25-2016, 11:12 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You want to print this, or put it into a variable? If the former then it is three nested cycle and one printf
 
1 members found this post helpful.
Old 07-25-2016, 11:16 PM   #5
conejo_perez16
LQ Newbie
 
Registered: Aug 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
ok thanks i will try tomorrow morning and put the code here if i get stuck again at some point
 
Old 07-26-2016, 12:11 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 07-26-2016, 08:25 AM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by conejo_perez16 View Post
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
 
Old 07-26-2016, 10:34 AM   #8
conejo_perez16
LQ Newbie
 
Registered: Aug 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
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!
 
Old 07-26-2016, 11:35 AM   #9
gda
Member
 
Registered: Oct 2015
Posts: 130

Rep: Reputation: 27
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
 
1 members found this post helpful.
Old 07-26-2016, 12:13 PM   #10
conejo_perez16
LQ Newbie
 
Registered: Aug 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks a lot gda! it works!
 
Old 07-26-2016, 12:27 PM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,676

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
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?
 
1 members found this post helpful.
Old 07-26-2016, 12:53 PM   #12
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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?

Last edited by keefaz; 07-26-2016 at 12:55 PM.
 
1 members found this post helpful.
Old 07-26-2016, 12:57 PM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,676

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
Think about how nested loops work. The only thing different about column 2 is that it always is incremented by 12.
 
Old 07-26-2016, 01:09 PM   #14
gda
Member
 
Registered: Oct 2015
Posts: 130

Rep: Reputation: 27
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?
 
Old 07-26-2016, 01:22 PM   #15
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,676

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

Only the OP knows...
 
  


Reply

Tags
bash, loop, table


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Is it possible to create a bash script to create multiple new bash scripts Batistuta_g_2000 Linux - Newbie 6 02-19-2013 11:42 AM
[SOLVED] MySQL can't create table fakie_flip Programming 1 11-24-2012 10:14 PM
create partition table vbx_wx Linux - Newbie 9 07-28-2010 10:56 AM
how we can create table in bash shell scripting ravidangaych Linux - General 2 06-20-2009 01:42 AM
create an error table? finding strings, and counting... in bash elinenbe Programming 3 06-26-2008 03:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration