I have a file pk.txt which has pk data in following format
TableName | PK
Employee | id
Contact|name,country
My Output should be
Employee | t1.id=s.id
Contact| t1.name=s.name AND t1.country=s.country
I started of like this:
for LIST in `cat pk.txt` do
TABLE_NAME=`cut -d '|' -f1 ${LIST}`
P_KEY=`cut -d '|' -f2 ${LIST}`
P_KEYCNT=`echo $P_KEY |awk -F\, '{print NF-1}'`
START=0
for (( c=$START; c<=$P_KEY_CNT; c++ ))
do
##Need some Help Here##
done
I need some sort of dynamic variable to be generated based on number of keys and then build a string
Can't help you with your own approach, but following Perl code does exactly what you want:
genjoin.pl
#!/usr/bin/perl
use strict;
use warnings;
while (<>) {
chomp;
next if $_ =~ /TableName/;
$_ =~ s/\s+//g;
my ($tab, $cols) = split(/\|/, $_, 2);
my @cols = split(/,/, $cols);
for my $col (@cols) {
$col="t1.$col=s.$col";
}
print "$tab | " . join(" AND ", @cols) . "\n";
}
Run it like so:
$ perl genjoin.pl pk.txt
If you like the result, then simply redirect the output to a file: