0

I have these video files;

Fringe.S03E01.mkv 
Fringe.S03E02.mkv 
Fringe.S03E03.mkv 
etc...

I also have a .csv text file of the episode's titles;

01,Olivia
02,The Box
03,The Plateau
etc...

what I am trying to get to is;

Fringe.S03E01.Olivia.mkv
Fringe.S03E02.The Box.mkv
Fringe.S03E03.The Plateau.mkv
etc...

Ive been trying to figure out the syntax for a bash script using foreach, but not getting any useable results..

  • "I have a list" -- In what format? Is this a file or an array or ...?; – glenn jackman Sep 28 '18 at 22:55
  • the files are just video files, the titles are a plain text file – user88710 Sep 28 '18 at 23:07
  • Hello @user88710. you have spaces between The and box ? The and Plateau? – user88036 Sep 28 '18 at 23:21
  • No surprise - foreach is not a bash command. – RudiC Sep 29 '18 at 8:18
  • To anyone reading this question: there was no mention of ".csv text file" in the original OP's. That is just a sample of flagrant edit abuse by privileged users. Keep that in mind before wondering why the answers don't handle an actual csv file, with quoting, double quoting, CRLF, etc. – mosvy Sep 29 '18 at 17:57
4
fringe_copy(){
        local IFS=,
        while read num title; do
                f=Fringe.S03E$num.mkv
                if [ -f "$f" ]; then
                        echo mv "$f" "${f%.mkv}.$title.mkv"
                fi
        done
}
fringe_copy <list_of_files

You'll have to remove the echo from before mv if you're satisfied with the dry run.

| improve this answer | |
  • awesome awesome, thank you mosvy!! I understand most of what you're doing here except for the "${f%.mkv}".... – user88710 Sep 29 '18 at 0:19
  • @user88710 It just strips the .mkv extension. Look here, search for "Remove Smallest Suffix Pattern". – mosvy Sep 29 '18 at 0:28
1

Try also this awk proposal

ls Fringe* |
awk '
NR == FNR       {EP[$1]=$2
                 next
                }
                {IX=substr($2, length($2)-1, 2)
                 $2 = $2 OFS EP[IX]
                }
1
' FS=, episodes.csv FS=. OFS=. -
Fringe.S03E01.Olivia.mkv
Fringe.S03E02.The Box.mkv
Fringe.S03E03.The Plateau.mkv

After having read the episode titles file into the replacement array, it acts upon the ls results and inserts the respective title based on the number in the last two chars of $2.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.