Insert the line number from text file to filename output

Hi everyone :slight_smile:

I have a file "words.txt" containing hundreds of lines of text. Each line contains a slogan.

Using the code below i am able to generate an image with the slogan text from each line.

The image filename is saved matching the last word on each line.

Example:
Line 1: We do great things > things.jpg
Line 2: Make it happen > happen.jpg
Line 3: Go the extra mile > mile.jpg

Is it possible to save the image filename as the line number from the text file ?

What i would like is this:
Line 1: We do great things > 1.jpg
Line 2: Make it happen > 2.jpg
Line 3: Go the extra mile > 3.jpg

I would greatly appreciate some help. Thank you!

cat words.txt | \
while read word; do
  convert  canvas.jpg \
\( -size 350x350 -background none \
-fill white -gravity center caption:"$word" -trim +repage -depth 8 \) \
-gravity center -composite $word.jpg
done

Try this :

while read word; do
i=$((i+1))
  convert  canvas.jpg \
\( -size 350x350 -background none \
-fill white -gravity center caption:"$word" -trim +repage -depth 8 \) \
-gravity center -composite $i.jpg
done < words.txt

cat and pipe is not needed here.

Regards
Peasant.

1 Like

Hi Peasant,

Thank you so much for saving my day. Your code worked perfectly!

Much appreciated. many thanks again! :smiley: