Insert the line number from text file to filename output


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Insert the line number from text file to filename output
# 1  
Old 10-11-2018
Insert the line number from text file to filename output

Hi everyone Smilie

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!

Code:
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

# 2  
Old 10-11-2018
Try this :
Code:
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.
This User Gave Thanks to Peasant For This Post:
# 3  
Old 10-11-2018
Hi Peasant,

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

Much appreciated. many thanks again! Smilie

Last edited by Don Cragun; 10-12-2018 at 12:33 AM.. Reason: Remove duplicated text.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert text at the beginning of every even number line

i am trying to insert text at the beginning of every even number line with awk i can do it with odd number lines with this command awk 'NR%2{$0="some text "$0}1' filehow can i edit this command thanks (5 Replies)
Discussion started by: bob123
5 Replies

2. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

3. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

4. UNIX for Dummies Questions & Answers

Insert a line in a text file

I want to insert a line with text after the 9th line of a text file. How would I do this using sed or awk? (2 Replies)
Discussion started by: lost.identity
2 Replies

5. Shell Programming and Scripting

Insert a variable to a text file after fixed number of lines

Hi, I am new to unix. I need to insert a variable which contains some lines of text into a text file after fixed number of lines.. Please help me on this.. Thanks in Advance, Amrutha (3 Replies)
Discussion started by: amr89
3 Replies

6. Shell Programming and Scripting

Insert output into file at line number

I need to insert the output of a script into a specific line number of a txt file. I've read the Sed man page and searched the forums and it's not immediately clear how I would go about doing this. (4 Replies)
Discussion started by: pluto7777
4 Replies

7. Shell Programming and Scripting

Insert text at line number

I wrote a script to grep for a closing XML node. Then I need it to navigate up a line and insert some XML. Then go to the next occurrance. I have this INSERT_NODE='<QUANTITATIVE NAME="'${QR_NAME}'" QUANT="1" />' GREP_FOR='</JOB>' TMP_FILE=/tmp/lineArray.$$ if ]; then continue else ... (7 Replies)
Discussion started by: J-Man
7 Replies

8. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

9. UNIX for Dummies Questions & Answers

How to grep / zgrep to output ONLY the matching filename and line number?

Hi all, I am trying to zgrep / grep list of files so that it displays only the matching filename:line number and does not display the whole line, like: (echo "1.txt";echo "2.txt") | xargs zgrep -no STRING If I use -o option, it displays the matching STRING and if not used, displays the... (3 Replies)
Discussion started by: vvaidyan
3 Replies

10. Shell Programming and Scripting

Insert text file at a certain line.

I need to insert a file called temp_impact (which has about 15 lines in it) to a file called 11.23cfg starting at line 33. I searched the forums and found the sed '34i\ test' 11.23cfg > newfile That will enter word test at the appropriate line, but i need the entire file dumped there. Any... (4 Replies)
Discussion started by: insania
4 Replies
Login or Register to Ask a Question