Generate Join clause based on key data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generate Join clause based on key data
# 1  
Old 10-13-2014
Generate Join clause based on key data

Hi,

I have a file pk.txt which has pk data in following format

Code:
TableName | PK
Employee | id
Contact|name,country

My Output should be
Code:
Employee | t1.id=s.id
Contact| t1.name=s.name AND t1.country=s.country

I started of like this:

Code:
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

Thanks
Wah
# 2  
Old 10-13-2014
Can't help you with your own approach, but following Perl code does exactly what you want:

genjoin.pl
Code:
#!/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:
Code:
$ perl genjoin.pl pk.txt

If you like the result, then simply redirect the output to a file:
Code:
$ perl genjoin.pl pk.txt >outfile.txt

Hope this helps.
# 3  
Old 10-13-2014
You already use awk to get your KEYCNT so why not use awk to do all the processing:

Code:
awk -F "[ |]*" '
NR>1{ 
  printf $1 " | "
  flds=split($2, F, ",")
  for(i=0;i<flds;i++)
     printf "%s%s.%s=%s.%s", i?" AND ":"", S, F[i+1], D, F[i+1]
  printf "\n"
}' S=t1 D=s pk.txt

Note NR>1 is to skip the "Table| PK" header line. I set variables S (source) and D(Dest) so you could change them easily
This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 10-14-2014
Another approach
Code:
awk '{gsub(/[^,]+/, "t1.&=s1.&", $2); gsub(/,/," AND ", $2)} NR>1' FS=' *[|] *' OFS=\| file


Last edited by Scrutinizer; 10-14-2014 at 02:35 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 10-14-2014
Code:
perl -F'\|' -wlap -e 'if($#F+1 && $.>1){$F[1] = join(" AND ", map{ s/\s*(\S+)\s*/t1.$1=s.$1/;  $_ } split(",",$F[1]) );$_ = join("|",@F)}' file

---------- Post updated at 10:35 PM ---------- Previous update was at 08:35 PM ----------

OR like this

Code:
perl -F'\|' -lape 'BEGIN{$,="|"}next if $. < 2 || !($#F+1); $F[1] = join(" AND ",map{ s/^\s+|\s+$//g; "t.".$_."=s.".$_ } split(/,/,$F[1])); $_=join($,,@F)' file

# 6  
Old 10-14-2014
Another way using just substitution:

Code:
perl -wlne 'print if /Employee/ and s/(\|\s*)(\w+)$/$1tl.$2=s.$2/ or /Contact/ and s/(\|\s*)(\w+),(\w+)$/$1t1.$2=s.$2 AND t1.$3=s.$3/;' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

2. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

3. Shell Programming and Scripting

Join 2 CSVs based on 1 key

Hello, I have 2 csv as follows: a.csv: name,phone,adress,car xy,1234,asbd yz,2134,asbdf tc,6789,salkdur b.csv: telphone,vehicle 2134,toyota 6789,bmw 1234,honda What is need is this: output.csv: name,phone,adres,car xy,1234,asbd,honda yz,2134,asbdf,toyota (7 Replies)
Discussion started by: Zam_1234
7 Replies

4. Shell Programming and Scripting

Generate tabular data based on a column value from an existing data file

Hi, I have a data file with : 01/28/2012,1,1,98995 01/28/2012,1,2,7195 01/29/2012,1,1,98995 01/29/2012,1,2,7195 01/30/2012,1,1,98896 01/30/2012,1,2,7083 01/31/2012,1,1,98896 01/31/2012,1,2,7083 02/01/2012,1,1,98896 02/01/2012,1,2,7083 02/02/2012,1,1,98899 02/02/2012,1,2,7083 I... (1 Reply)
Discussion started by: himanish
1 Replies

5. UNIX for Advanced & Expert Users

How to generate expiring SSH Key ?

Hi Gurus, I am stuck with a problem here for which I need your expert advice. I need to generate an SSH key in my Sun OS machine which should expire in 2 years. I usually generate the keys using ssh-keygen -t dsa but the keys generated like this would be non-expiring. I checked for the... (2 Replies)
Discussion started by: nathsaba
2 Replies

6. Shell Programming and Scripting

Left Join in Unix based on Key?

So I have 2 files: File 1: 111,Mike,Stipe 222,Peter,Buck 333,Mike,Mills File 2: 222,Mr,Bono 444,Mr,Edge I want output to be below, where 222 records joined and all none joined records still in output 111,Mike,Stipe 222,Peter,Buck,Mr,Bono 333,Mike,Mills 444,Mr,Edge (4 Replies)
Discussion started by: stack
4 Replies

7. Shell Programming and Scripting

generate report based on data in files.

Hi All, I need to develop a shell script which does sanity check of a data file, as below. 1. For DATE columns, it should check if date is given in proper format or not? For example, if format of date is expected as DD-MON-YYYY HH24:MI:SS and we received the date in formation like DDMMYYYY HH24,... (1 Reply)
Discussion started by: ace_friends22
1 Replies

8. Solaris

Help Generate SSL key in solaris.

How do I generate an SSL string in solaris 10? (3 Replies)
Discussion started by: Kjons76
3 Replies

9. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

10. UNIX for Advanced & Expert Users

Relational Join (Composite Key)

hi, i have file 1: ====== 0501000|X1 0502000|X2 0501231|X3 0981222|X4 0502000|X6 0503000|X7 0932322|X8 file 2: ======= 050 0501 0502 09 098 (1 Reply)
Discussion started by: magedfawzy
1 Replies
Login or Register to Ask a Question