Home > Back-end >  Generate a series of numbers that go up in two interweaving intervals
Generate a series of numbers that go up in two interweaving intervals

Time:02-12

Let's say I want to generate a series of numbers that first jump 5 and then 10 all the way to 100 which would be

0,5,15,20,30,35,45,50,60,65,75,85,90,100

I am aware of

seq 0 10 100

but am looking to jump two different intervals intertwined.

CodePudding user response:

As bash is tagged: Use seq with a step size that covers a whole cycle (here 5 10 = 15). Then, for each line, print the missing steps.

seq 0 15 100 | while read -r n; do printf '%d\n' $n $((n 5)); done
0
5
15
20
30
35
45
50
60
65
75
80
90
95

If the intervals are more complex, calling seq that way could very well be nested again inside, like so for this example

seq 0 15 100 | while read -r n; do seq $n 5 $((n 5)); done

CodePudding user response:

You may use 2 printf statements to generate 2 lists and then sort the combined list using sort -n:

{ printf '%d\n' {0..100..15}; printf '%d\n' {5..100..15}; } | sort -un
0
5
15
20
30
35
45
50
60
65
75
80
90
95

CodePudding user response:

Assumptions:

  • start with 0
  • generate a sequence of numbers by adding 5, 10, 5, 10 ... until we reach/exceed 100

One idea using a bash loop:

i=0
sum=0

while [[ "${sum}" -le 100 ]]
do
    echo $sum
    ((sum  = (i  %2   1) * 5))
done

This generates:

0
5
15
20
30
35
45
50
60
65
75
80
90
95

CodePudding user response:

$ cat tst.awk
BEGIN {
    for ( i=0; i<=100; i =(  c%2 ? 5 : 10) ) {
        print i
    }
}

$ awk -f tst.awk
0
5
15
20
30
35
45
50
60
65
75
80
90
95
  • Related