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.

I want to produce a following list of pages (for imposition work); (number of pages is variable, but I make a sample of my needs using a list of 16 pages, logic is the same for longer lists)

1,16
8,9
15,2
10,7
3,14
6,11
13,4
12,5

I written this for cycle:

for ((x=16, y=1, z=8, w=9;x>=4, y<=4, z>=12, w<=12;x--, y++, z--, w++)); do echo "$x $y;$z $w"; done | tr ";" "\n" | tr " " ","

that, however, produces:

16,1
8,9
15,2
7,10
14,3
6,11
13,4
5,12

and not the list of pages I want. So, I need, with awk or other commands, to swap fields in some columns following this pattern (repeating every 4 couple of pages)

swap fields

no swap fields

no swap fields

swap fields

16,1 (swap: becomes: 1,16
8,9  (no swap: remains 8,9
15,2 (no swap: remains 15,2
7,10 (swap: becomes: 10,7
14,3 (swap: becomes: 3,14
6,11 (no swap: remains 6,11
13,4 (no swap: remains 13,4
5,12 (swap: becomes: 12,5

generalizing the logic of this imposition (I need to impose a single copy of an A6 book across an A4 page format) I wrote a more generalized script

#!/bin/sh
pages=$1
halfpages="`let DIVISION=$pages/2; echo $DIVISION`"
incr="$(echo "scale=0; $halfpages+1" |bc -l)"
dividedby4="$(echo "scale=0; $pages/4" |bc -l)"
lastupperpage="$(echo "scale=0; $pages-$dividedby4" |bc -l)"
u="u"
for ((x=$pages, y=1, z=$halfpages, w=$incr;x>=4, y<=4, z>=$lastupperpage, w<=$lastupperpage;x--, y++, z--, w++)); do echo "$x$u $y$u;$z $w"; done | tr ";" "\n" | tr " " "," | tr "\n" ","
exit 0

however I'm only able to produce:

16u,1u,8,9,15u,2u,7,10,14u,3u,6,11,13u,4u,5,12

instead of right:

1u,16u,8,9,15u,2u,10,7,3u,14u,6,11,13u,4u,12,5

that I pass to Multivalent.jar

I attach a picture of right imposition of an A6 across an A4 to show the right sequence of imposition

enter image description here

what I try to obtain is a way to impose a whole A6 book across an A4 page in ONE signature consisting in all its pages (not the 8-Page Imposition), in order to cut at half A4 page, close the two halves one over another, reverse sheet obtained, cut at half and applying glue to left edge in order to apply cover

to perform this task I need or to rethink my script from scratch or find a way to swap fields in columns by a fixed pattern (swap, no swap, no swap, swap) in order to correct wrong order produced by my script

EDIT

solved sending standard output of script to awk with this syntax:

awk -F "," '{
    print $2 "," $1;
    getline; print;
    getline; print;
    getline; print $2 "," $1
}' 
share|improve this question
    
well. I added to original question, a more generalized script to show logic of this imposition. I need to automatize the process of imposing an A6 book across an A4 page (4 A6 pages into any face of an A4 page). total pages to impose are ever a multiple of 8 - refresh page to view my edits to original question –  Dingo Apr 15 '12 at 23:21
    
Hmm, the page reload must have been slow for me. I still don't understand why you want to use so much code when you could just print 1u,16u,8,9,15u,2u,10,7,3u,14u,6,11,13u,4u,12,5 directly. –  jw013 Apr 15 '12 at 23:29
    
because is simple for 8, 16 32 or, at least, 64 pages, write manually imposition orders, but I need automation for longer books (more than 200 pages). I attached a picture I made to show the right imposition sequence for a 16 page A6 book across an A4 (logic is the same for longer books) –  Dingo Apr 15 '12 at 23:35
    
I'm probably misunderstanding things but for a 200 page book do you really want page 1 and page 200 adjacent on the same sheet of paper? If you are imposing A6 on A4, that's 8 pages of A6 per sheet of A4 (4 per side). How would the binding even work in that case? –  jw013 Apr 15 '12 at 23:59
    
yes, as I written in a further edit to my question, for reasons of economy in printing and binding I do not want use 8-Page Imposition that implies folding of pages in groups of 8, but I want make a single signature of all pages (like in A5 booklet) that I can cut and apply then the glue without needing of folding. if you create your own model in paper of my imposition you'll see better what I mean –  Dingo Apr 16 '12 at 0:11
show 1 more comment

3 Answers

up vote 3 down vote accepted

The following script is a much cleaner way of obtaining the output you want.

#!/usr/bin/env bash

(($# == 0)) && {
    echo "Usage: $0 <page_count>"
    exit 1
} >&2

# nTotal = $1 rounded up to a multiple 8
nTotal=$(($1 + 7))
nTotal=$((nTotal - nTotal % 8))
nHalf=$((nTotal / 2))
nQuarter=$((nTotal / 4))

# print page numbers in groups of 4, 2 groups at a time
for ((x=0; x < nQuarter; x+=2)); do
    printf '%du,%du\n%d,%d\n' \
        $((x + 1)) $((nTotal - x)) $((nHalf - x)) $((nHalf + x + 1))
    printf '%du,%du\n%d,%d\n' \
        $((nTotal - (x + 1))) $((x + 2)) \
        $((nHalf + x + 2)) $((nHalf - x - 1))
done

Example

$ ./imposition8 16
1u,16u
8,9
15u,2u
10,7
3u,14u
6,11
13u,4u
12,5

I rewrote your script because there were several problems with the original.

  • You used non-sh compatible bash specific syntax, so I corrected the shebang line to match.

  • The entire thing can be done in pure bash. The use of multiple tr invocations, an external awk, and bc is inefficient. In particular, you should know that bash and even POSIX-compliant sh can do math internally.

share|improve this answer
    
Yes, I know it was my fault using inefficient and duplicate invocations of same command. thanks for tips –  Dingo Apr 16 '12 at 11:22
add comment

Here is a script which does it cleanly (hopefully).. I'm not up with printing/binding methods, so I'm not sure if the sequencing is correct, but it certainly works from the center out... I just don't knwo the folding/cutting sequence you require (beyond your 16 page example)... I've included a sample 24 page output.

pages=${1:-16}
pagesPerSheet=4
sheets=$((  (pages/pagesPerSheet) 
          +((pages%pagesPerSheet)>0) ))
pagesTot=$((sheets*pagesPerSheet))

Ob=0               # overall begin
Ox=$((pagesTot+1)) # overall max 
Hb=$((Ox/2))       # 2nd Half begin
hx=$((Hb+1))       # 1st half max

for ((s=sheets;s>0;s-=2)) ;do
   printf '%su,%su,%s,%s,%su,%su,%s,%s%s' \
           $((Ob+=1)) $((Ox-=1)) \
           $((hx-=1)) $((Hb+=1)) \
           $((Ox-=1)) $((Ob+=1)) \
           $((Hb+=1)) $((hx-=1)) \
           $(((s>2)) && echo ,)
done; echo  

output

# 16 page
1u,16u,8,9,15u,2u,10,7,3u,14u,6,11,13u,4u,12,5
# 24 page
1u,24u,12,13,23u,2u,14,11,3u,22u,10,15,21u,4u,16,9,5u,20u,8,17,19u,6u,18,7

This code snippet prints a graphical view

Ob=0               # overall begin
Ox=$((pagesTot+1)) # overall max 
Hb=$((Ox/2))       # 2nd Half begin
hx=$((Hb+1))       # 1st half max

for ((s=sheets;s>0;s-=2)) ;do
   printf '%2su %2su | %2su %2su\n%2s  %2s  | %2s  %2s\n-------   -------\n' \
           $((Ob+=1)) $((Ox-=1)) $((Ox-=1)) $((Ob+=1)) \
           $((hx-=1)) $((Hb+=1)) $((Hb+=1)) $((hx-=1))
done

output

# 16 page
 1u 16u | 15u  2u
 8   9  | 10   7
-------   -------
 3u 14u | 13u  4u
 6  11  | 12   5
-------   -------

# 24 page
 1u 24u | 23u  2u
12  13  | 14  11
-------   -------
 3u 22u | 21u  4u
10  15  | 16   9
-------   -------
 5u 20u | 19u  6u
 8  17  | 18   7
-------   -------
share|improve this answer
add comment

I understand the desire to derive a formula, but wouldn't it be much more easy, and therefore better, from a maintenance perspective, to use something like this:

#!/bin/bash
arr=(1 16 8 9 15 2 10 7 3 14 6 11 13 4 12 5)
for i in {0..3}; do 
    for n in ${arr[*]}; do 
        echo -n $((n+16*i))","
    done
    echo
done | sed -r 's/([^,]+,[^,]+),/\1\n/g' 

Or doesn't the pattern repeat like this:

1,16
8,9
15,2
10,7
3,14
6,11
13,4
12,5

17,32
24,25
31,18
26,23
19,30
22,27
29,20
28,21

...
share|improve this answer
    
That was my initial suggestion as well, but after a long comment exchange I came to believe that the desired order is to have page 1 always adjacent to the last page (rounded to multiple of 8). –  jw013 Apr 16 '12 at 11:35
    
Multiples of 8, or powers of 8? I can't see how the logic has to be kept with bigger numbers. It should be better explained in the question, not only in comments (logic is the same for longer lists). –  user unknown Apr 16 '12 at 14:09
3  
There are a few printing/binding jargon words which got my head spinning for a while.. The key word for me is signature (in an update, the OP, emphasizes the whole book in ONE signature")... a "signature" is a unit within a book which contains a range of pages, folded over as a single group. The smallest signature is a single piece of paper with pages 1,2,3,and 4 on it.. folded at the center to make a 4-page book, etc.. The only limiting factor is how many times can you (physically) reasonably keep adding to the *signature.. I guess that's up to the book-binder/printer. –  Peter.O Apr 16 '12 at 18:22
add comment

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.