Trouble using awk and sed commands

Hi i have a control file which i need to read. It is ',' separated. the 3rd parameter will be ';' separated.
I have 2 files:
/home/orig.txt
/home/join.txt
I need a O/P file name based on firstparameter_1.txt and it should have the content of /home/orig.txt
and appended content from /home/join.txt where it finds keywords from last parameter.
While copying all the content from orig to O/P fiel it should check for any entries of keywords from last parameter and delete whole line if there is an entry
Lines should be appeneded immediately after it finds 'record' key word.

controlfile.txt
---------------
boy,/home/orig.txt,/home/join.txt,ravi;phani

conetent of /home/orig.txt
---------------------------
record
he is good
he is bad
end
conetent of /home/join.txt
--------------------------
record
ravi is boy
phani is boy
apurva is girl
pushpa is girl
sneha is girl
end
O/P
___
file name:boy_a.txt
content of boy_a.txt:
---------------------
record
ravi is boy
phani is boy
he is good
he is bad
end

__________________________________________________________________

controlfile.txt
---------------
girl,/home/orig.txt,/home/join.txt,apurva;pushpa;sneha
conetent of  /home/orig.txt
----------------------------
record
he is good
he is bad
end
conetent of /home/join.txt
---------------------------
record
ravi is boy
phani is boy
apurva is girl
pushpa is girl
sneha is girl
end
O/P:
___
file name:girl_a.txt
content of girl_a.txt:
-----------------------
record
apurva is girl
pushpa is girl
sneha is girl
he is good
he is bad
end

The last parameter frm the control file can vary from 1 to n and it will be ; separated if it has greater than 2 entries.

while read name,orig,append,paralist
do
cp /home/orig.txt $name_a.txt
echo "$paralist" > /home/temporary.txt
awk -F";" '{
for (i=1; i<NF; i++) 
{
cat echo $orig | grep -i $i >> /home/temp.txt
sed -ni '/\($i\)/p' ${orig}
}
}' /home/temporary.txt
sed -i '/record/r /home/temp.txt' $name_a.txt
done<controlfile.txt

Based on assumptions:

awk '
        BEGIN {
                FS = "[ ,]"
        }
        NR == FNR {
                F = $1
                n = split ( $NF, R, ";" )
                for ( i = 1; i <= n; i++ )
                        C[R] = F
                next
        }
        FILENAME == "join.txt" {
                if ( $1 in C )
                        O[++c] = $0
                next
        }
        FILENAME == "orig.txt" {
                if ( $0 ~ /record/ )
                {
                        print > F "_a.txt"
                        for ( i = 1; i <= c; i++ )
                                print O > F "_a.txt"
                }
                else
                        print > F "_a.txt"
        }
' controlfile.txt join.txt orig.txt

Try

awk -F, '               {FN=$1"_a.txt"
                         NN=split ($NF, NAMES, ";")
                         getline X < $2; print X > FN
                         while (getline X < $3) for (i=1; i<=NN; i++) if (match (X, NAMES".*"$1)) print X > FN
                         while (getline X < $2)  print X > FN
                        }
        ' controlfile
record
apurva is girl
pushpa is girl
sneha is girl
he is good
he is bad
end
1 Like