Cut line from searched file if grep find neighbor columns

Hello All,
While searching for the question, I found some answers but my implementation is not giving expected output.
I have two files; one is sourcefile , other is named template .
What I want to do is to search each line in template , when found all columns , cut the matching line from source file.
Print order is important. I have tested another script but it gives the output in alphabetical order which I do not desire.
If column1 and column2 in template file are not neighbor in sourcefile, I wish the script to skip that line. Max column nr is: 5

sourcefile

Monday Tuesday Wednesday
Monday Friday 998877
Tuesday Friday 112233
Tuesday Friday 112233 11223344
Monday 998877 012345678
Tuesday Friday
Thursty

template

Monday 998877
Tuesday Friday
Thursday Friday
Saturday
Sunday
Monday Tuesday Thursday Friday

Expected output with below order:

Tuesday Friday 112233
Tuesday Friday 112233 11223344
Monday 998877 012345678
Tuesday Friday

Tested script:

while read -r COL1 COL2 COL3 COL4 COL5
do
if grep -q "$COL1\|$COL2\|$COL3\|$COL4\|$COL5" sourcefile; then
    grep -Ff sourcefile template
fi
done<template

I'd like to know how to manage this process.

Thanks
Boris

how exactly did you get Monday Tuesday Thursday Friday in your desired output?

Hello,
My fault. I am gonna update the update now.
Thanks for the warning

Boris

grep -F -f template sourcefile

1 Like

Dear Vgersh99,
Thank you so much!

Kind regards
Boris