title: Parallelize nested for loop in GNU Parallel
body: <p>I have a small bash script to OCR PDF files (slightly modified <a href="https://help.ubuntu.com/community/OCR" rel="noreferrer">this</a> script). The basic flow for each file is:</p>
  
  <p>For each page in pdf FILE:</p>
  
  <ol>
  <li>Convert page to TIFF image (imegamagick)</li>
  <li>OCR image (tesseract)</li>
  <li>Cat results to text file</li>
  </ol>
  
  <p>Script:</p>
  
  <pre><code>FILES=/home/tgr/OCR/input/*.pdf
  for f in $FILES
  do
  
    FILENAME=$(basename "$f") 
    ENDPAGE=$(pdfinfo $f | grep "^Pages: *[0-9]\+$" | sed 's/.* //')
    OUTPUT="/home/tgr/OCR/output/${FILENAME%.*}.txt"
    RESOLUTION=1400
    touch $OUTPUT
    for i in `seq 1 $ENDPAGE`; do
        convert -monochrome -density $RESOLUTION $f\[$(($i - 1 ))\] page.tif
        echo processing file $f, page $i
        tesseract page.tif tempoutput -l ces
        cat tempoutput.txt &gt;&gt; $OUTPUT
    done
  
    rm tempoutput.txt
    rm page.tif
  done
  </code></pre>
  
  <p>Because of high resolution and fact that tesseract can utilize only one core, the process is extremely slow (takes approx. 3 minutes to convert one PDF file).</p>
  
  <p>Because I have thousands of PDF files I think I can use <code>parallel</code> to use all 4 cores, but I don't get the concept how to use it. In <a href="http://www.gnu.org/software/parallel/man.html#example__rewriting_nested_for_loops" rel="noreferrer">examples</a> I see:</p>
  
  <pre><code>Nested for-loops like this:
  
    (for x in `cat xlist` ; do
      for y in `cat ylist` ; do
        do_something $x $y
      done
    done) | process_output
  can be written like this:
  
  parallel do_something {1} {2} :::: xlist ylist | process_output
  </code></pre>
  
  <p>Unfortunately I was not able to figure out how to apply this. How do I parallelize my script?</p>