Search code examples
bashshellpdfcopyimagemagick-convert

Read data from a text file and convert images to PDF referred to it


I have a text file with this content:

123412-01
123413-01
123411-01
123414-01
123415-01

I would like to write a script (Bash/shell/command line or PHP) that will select the 6 first digits for each line and copy the files in an other directory that contains images named like that.

123412-123.jpg
123412-124.jpg
123412-123.jpg
123413-123.jpg
123414-123.jpg
123415-123.jpg
123416-123.jpg

I don't know if I'm clear in my question.

  1. Read the first line and select the 3 first digits (keep it in memory)

  2. Read the first line and now select the 6 first digits (keep it in memory)

  3. Convert the images (in a second directory) that contain the 3 first digits and the nine 6 in folder and subfolder. For Example using the command:

     convert /blabla/Images/H(3 first digits)/(6digits)-*.jpg /test/PDF/(6digits)-01.pdf 
  4. Read the second line...

I have write this code to try it but it's not working.


    cat id.txt
    sF= cut -b 1-3 id.txt
    F=cut cut -b 1-6 id.txt

    while read -r line ; 

    do

    convert /blabla/Images/H$sF/$F.jpg /test/PDF/'$F'-01.pdf

    done

I think I'm doing wrong with the variable and the path but I can't find any solution to solve it.


Solution

  • I think you mean this:

    #!/bin/bash
    while read line; do
       first3=${line:0:3}
       first6=${line:0:6}
       echo convert "/blah/blah/images/H${first3}/${first6}-*.jpg" "/test/PDF/${first6}-01.pdf"
    done < file.txt
    

    which gives this:

    Sample Output

    convert /blah/blah/images/H123/123412-*.jpg /test/PDF/123412-01.pdf
    convert /blah/blah/images/H123/123413-*.jpg /test/PDF/123413-01.pdf
    convert /blah/blah/images/H123/123411-*.jpg /test/PDF/123411-01.pdf
    convert /blah/blah/images/H123/123414-*.jpg /test/PDF/123414-01.pdf
    convert /blah/blah/images/H123/123415-*.jpg /test/PDF/123415-01.pdf