Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I generate the following increments?

2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31,34,35,38,39,42,43,46,47,...,135

and

4,5,8,9,12,13,16,17,20,21,24,25,28,29,32,33,36,37,40,41,44,45,48,49,...,137

They are needed when printing pages. I can create incrementally counting "one liners" in bash, but I can't write one for this. Can anybody help me out?

share|improve this question
3  
This seems like an XY problem. What are you actually trying to do? –  John Flatness Nov 12 '11 at 6:35
    
What are you printing? If it's postscript, you could use psselect from psutils to just print the pages you want. If it's an ascii file you could use a2ps and then psselect. –  ConcernedOfTunbridgeWells Nov 12 '11 at 17:34

4 Answers 4

up vote 2 down vote accepted

This works. Save it to increment.bash then type bash increment.bash 2 to get the incrementing starting at 2, or bash increment.bash 4 to start at 4. Will start at 2 if none defined. (Made some refinements to where the previous actually works)

#!/bin/bash

if [ $1 ]; then
    opt=$1
    if [ $opt == "--help" ]; then echo "Usage: $0 [2|4] -- either starting number 2 or 4, to print first number and +1, increment by 3, then continue."; exit 0
    elif [ $opt == 2 ]; then i=$opt
    elif [ $opt == 4 ]; then i=$opt
    fi
else i=2
fi

echo $i; let i++; echo $i;

for (( n=1; $n < 35; n++ )); do
    if [ $i == 135 ] || [ $i == 137 ]; then break; fi
    i=$[i+3]; echo "$i";
    let i++; echo "$i";
done

Here it is in action:

$ for i in {2..4..2}; do for type in head tail; do echo ":: $i | $type :: "; bash increment.bash $i | $type; done; done
:: 2 | head :: 
2
3
6
7
10
11
14
15
18
19
:: 2 | tail :: 
118
119
122
123
126
127
130
131
134
135
:: 4 | head :: 
4
5
8
9
12
13
16
17
20
21
:: 4 | tail :: 
120
121
124
125
128
129
132
133
136
137
share|improve this answer

I'm not exactly sure what you're trying to do, but making something that just counts that sequence is pretty simple:

i=2 # or 4
while true; do
    echo $i
    i=$(($i+1))
    echo $i
    i=$(($i+3))
done
share|improve this answer

Using GNU sed for its BEGIN~STEP opiton.

printf "%s\n" {1..137} |tee \
    >(sed -nr '2~4H; 3~4H; ${g; s/\n/,/g; s/^,(.*)/\1/p}'>set23) |
      sed -nr '4~4H; 5~4H; ${g; s/\n/,/g; s/^,(.*)/\1/p}'>set45
cat set23
cat set45

Output

2,3,6,7,10,11,14,15, ... ,130,131,134,135
4,5,8,9,12,13,16,17, ... ,132,133,136,137 
share|improve this answer

Here's one way to build these lists of numbers: go through the numbers from 1 to $n, and append them to whichever list is appropriate. The snippet below uses only portable constructs.

recto= verso= i=1
while [ $i -le $n ]; do
  if [ $((i % 4)) -le 1 ]; then recto=$recto,$i; else verso=$verso,$i; done
  i=$((i + 1))
done
recto=${recto%,} verso=${verso%,}

Here's a ksh/bash/zsh version:

recto= verso=
for ((i=1; i<n; i++)); do
  if ((i % 4 <= 1)); then recto=$recto,$i; else verso=$verso,$i; done
done
recto=${recto%,} verso=${verso%,}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.