While read line loop

Based on text file:

PATH:/media/hdd/Media/Video/title1 FILE:/media/cache/281662-14.jpg
PATH:/media/hdd/Media/Video/title2 FILE:/media/cache/281662-15.jpg
PATH:/media/hdd/Media/Video/title3 FILE:/media/cache/281662-16.jpg
PATH:/media/hdd/Media/Video/title4 FILE:/media/cache/281662-17.jpg
PATH:/media/hdd/Media/Video/title5 FILE:/media/cache/281662-18.jpg
PATH:/media/hdd/Media/Video/title6 FILE:/media/cache/281662-19.jpg

I would like to create loop to execute some tasks for every line:

while read line
do
    I need to copy files from every line i.e. /media/cache/281662-19.jpg to other location but with name given in PATH: so the result will be like this: title6.jpg
done < $FILE

Thank you for help.

How about

while read P F; do echo cp ${F#*:} ${P#*:}; done < file
cp /media/cache/281662-14.jpg /media/hdd/Media/Video/title1
cp /media/cache/281662-15.jpg /media/hdd/Media/Video/title2
cp /media/cache/281662-16.jpg /media/hdd/Media/Video/title3
cp /media/cache/281662-17.jpg /media/hdd/Media/Video/title4
cp /media/cache/281662-18.jpg /media/hdd/Media/Video/title5
cp /media/cache/281662-19.jpg /media/hdd/Media/Video/title6

?
Remove the echo if happy with the result.

1 Like

Text file is just example and every line will be different apart from tag PATH: and FILE: which I added myself to make it easier.
Path after FILE: is path to existing file which has to stay in place as it is.
Path after PATH: shows name and new location of file from FILE:

In result I need these files:

/media/hdd/Media/Video/title1.jpg
/media/hdd/Media/Video/title2.jpg
/media/hdd/Media/Video/title3.jpg
/media/hdd/Media/Video/title4.jpg
/media/hdd/Media/Video/title5.jpg
/media/hdd/Media/Video/title6.jpg

I do not understand.

I have a jpg files in /media/cache/. Each jpg coresponds to its movie file as it its cover so I need to create rule which will copy jpg from cache to i.e. /media/video where movie exists. That copied jpg has to be also renamed and name is given after PATH: in that text file.
This rule has to do its job for every line in this file.
Hope this makes it more clear.

edit:

More or less it works with clue you gave me. I just modified my text file a bit:

sed -i 's/avi FILE/jpg FILE/g' $FILE
sed -i 's/mkv FILE/jpg FILE/g' $FILE
while read P F; do cp ${F#*:} ${P#*:}; done < $FILE

but now I have to find a way to copy files when space is inside the title. Now copy command just cut ater first occurence of space.

You'll need to find a way to tell field separators from spaces within file names. Can you create the file using <TAB> as separators?

To be honest I do not know how to do it. If I manually insert backspaces before spaces and run it in telnet session then it works but not with script.

How is the file posted in #1 created? By an application you have control of?

Yes. Before I issue this command

while read P F; do echo ${F#*:} ${P#*:}; done < $

I modify it with sed to look like in #1 but some titles have spaces:

PATH:/media/hdd/Media/Video/title number 1 FILE:/media/cache/281662-14.jpg
PATH:/media/hdd/Media/Video/title2 FILE:/media/cache/281662-15.jpg
PATH:/media/hdd/Media/Video/title3 FILE:/media/cache/281662-16.jpg
PATH:/media/hdd/Media/Video/title number 4 FILE:/media/cache/281662-17.jpg
PATH:/media/hdd/Media/Video/title5 FILE:/media/cache/281662-18.jpg
PATH:/media/hdd/Media/Video/title6 FILE:/media/cache/281662-19.jpg

Again: how is that file created - NOT modified! - ?

It is movie database created by python plugin. It is not big deal. I can remove spaces manually from filenames.

Are you entitled/able to modify that python plugin to create the file using different separators?

---------- Post updated at 16:34 ---------- Previous update was at 16:31 ----------

Howsoever, would this help:

while IFS=":" read P F1 F2; do echo cp $F2 ${F1% FILE*}.${F2#*.}; done < file
cp /media/cache/281662-14.jpg /media/hdd/Media/Video/title number 1.jpg
cp /media/cache/281662-15.jpg /media/hdd/Media/Video/title2.jpg
cp /media/cache/281662-16.jpg /media/hdd/Media/Video/title3.jpg
cp /media/cache/281662-17.jpg /media/hdd/Media/Video/title number 4.jpg
cp /media/cache/281662-18.jpg /media/hdd/Media/Video/title5.jpg
cp /media/cache/281662-19.jpg /media/hdd/Media/Video/title6.jpg
1 Like

I can modify python code. Will see what I can do. Thanks