0

I wrote a simple bash script as I need to split multiple pdfs into 2 pdfs on a regular basis. I need them to be split in the same sequence each time (pages 1-5 and 6 to last page). I need the newly split pdfs to be named uniquely so I can keep them apart (i.e. inv-1.pdf rec-1.pdf; inv-2.pdf rec-2.pdf, etc). My script only splits the 1 pdf when I have 5+ in the folder. Any suggestions are appreciated.

    #!/bin/bash
    for file in $(ls REQ*.pdf)

    do
        qpdf ${file} --pages . 1-5 -- inv-%d.pdf |\
        qpdf ${file} --pages . 6-z -- rec-%d.pdf |\
        echo >> ${file}
    done

'''

PonH
  • 11
  • 3
  • I recommend you check your script via [shellcheck](https://www.shellcheck.net/) ... – tink Aug 27 '21 at 22:01
  • And a useful debugging technique to see whether the looping happens: before the first `qpdf` line, insert an `echo ${file}` ... – tink Aug 27 '21 at 22:41

1 Answers1

0

Quick solution: Append | xargs echo to your ls command.

    #!/bin/bash
    for file in $(ls REQ*.pdf | xargs echo)

    do
        qpdf ${file} --pages . 1-5 -- inv-%d.pdf |\
        qpdf ${file} --pages . 6-z -- rec-%d.pdf |\
        echo >> ${file}
    done

Rationale: In your case, ls output is one filename per line. Your for loop picks up the filename in the same line only. The rest get ignored. You can read more about ls output behavior on this answer- What is the magic separator between filenames in ls output?

The pipe (|) redirects ls output to xargs which applies echo to generate space-separated file names on a single line. Read more about xargs on the man page

trunc8
  • 21
  • 5
  • @tink tried your suggestion, thank you! It still only works to separate 1 file into 2 pdfs. The loop does not work on more than 1 REQ file in the folder. – PonH Aug 30 '21 at 17:06
  • tried your suggestion, thank you! It still only works to separate 1 file into 2 pdfs. The loop does not work on more than 1 REQ file in the folder. – PonH Aug 30 '21 at 17:06
  • Is it incidentally naming all the output files with the same name? That would be a case of overwriting. You can try incrementing an index variable inside the loop and use this variable in the output filenames. – trunc8 Aug 30 '21 at 17:20
  • no i think it is simply creating split pdfs for 1 file only. How do I increment an index variable inside the loop? I rewrote the code to try qpdf --empty --pages "${file}" 1-5 -- inv-%d.pdf but I still only get 1 file output for all pdfs in the folder. – PonH Sep 01 '21 at 17:08