I need to pass to pdftk, a list of arguments constituted by strings repeated twice like:

A1-2 B1-2 A3-4 B3-4 A5-6 B5-6... and so on...

I managed to achieve this with this workaround

for ((x=1, y=2;x>=18, y<=18;x++, y++)); do echo "A$x-$y B$x-$y "; done| awk 'NR %2==1 {print $1, $2} ' | tr '\n' ' '

that gives:

A1-2 B1-2 A3-4 B3-4 A5-6 B5-6 A7-8 B7-8 A9-10 B9-10 A11-12 B11-12 A13-14 B13-14 A15-16 B15-16 A17-18 B17-18

but I'm unsatisfied, and I'm looking for a proper , more elegant and better way to perform this task

3 Answers 3

for ((x=1; x<=18; x+=2)); do echo -n "A$x-$((x+1)) B$x-$((x+1)) "; done
  • 1
    You can even use 2 variables, as in C: for (( x=1, y=2; x<=18; x+=2, y+=2 )); do printf "A%d-%d B%d-%d " $x $y $x $y; done Apr 11, 2012 at 13:12

or even pure awk:

awk 'BEGIN {for (i=1; i<19; i+=2) printf "A"i"-"i+1 " B"i"-"i+1" "}'

With just bash:

pdftk input.pdf cat $(for ((x=1; x<18; x+=2)); do echo {A,B}$x-$((x+1)); done) output output.pdf

With GNU utilities (Linux or Cygwin):

pdftk input.pdf cat $(seq 1 2 17 | awk '{$1 = $1 "-" ($1+1); print "A" $1; print "B" $1}') output output.pdf

With Perl:

pdftk input.pdf cat $(perl -e 'print map {"A$_ B$_ "} map {(2*$_-1) . "-" . (2*$_)} (1..9)') output output.pdf
  • +1 for offering valuable alternative one-liners. Maybe it would be worth considering to add xargs in order to not have too many arguments error, if number of strings becomes very big (100 or 200 or more)
    – Dingo
    Apr 11, 2012 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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