From: Cesear on
On Mar 18, 10:32 am, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
wrote:
> On Mar 18, 1:52 pm, Cesear <ces...(a)gmail.com> wrote:
>
>
>
> > Arjen u code works well, but I need to modify it some.  I need to be
> > able to join the final file and only show the unqiueid at the
> > beginning of the row.  How can I do that?
>
> Instead of repeating the question 4 times, just post the full example
> with inputs *and* wanted output.
>
> -Alex

Sorry must have hit the reply too many times :) Ok here is what I
want, just data no English :) --->>>

File ONE-->

V0100|NAME1|blah|blah|
V0102|NAME2|blah|blah|

File TWO-->

V0100|NAME1|chargeX|blah|
V0100|NAME1|chargeY|blah|
V0102|NAME2|chargeX|blah|
V0102|NAME2|chargeY|blah|
V0100|NAME1|blahcharge|blah|
V0102|NAME2|blahcahrge|blah|

The FINAL OUTPUT, I want to look like this-->

V0100|NAME1|blah|blah|chargeX|blah|
V0100|NAME1|blah|blah|chargeY|blah|
V0102|NAME2|blah|blah|chargeX|blah|
V0102|NAME2|blah|blah|chargeY|blah|
V0100|NAME1|blah|blah|blahcharge|blah|
V0102|NAME2|blah|blah|blahcahrge|blah|

From: Larry W. Virden on
On Mar 18, 10:32 am, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
wrote:

>
> Instead of repeating the question 4 times, just post the full example
> with inputs *and* wanted output.


I don't understand why, but I've been seeing repeating messages on
several usenet groups this week. While it could be user error, I am
beginning to suspect some sort of tech issue.

From: Cesear on
On Mar 18, 12:44 pm, "Larry W. Virden" <lvir...(a)gmail.com> wrote:
> On Mar 18, 10:32 am, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
> wrote:
>
>
>
> > Instead of repeating the question 4 times, just post the full example
> > with inputs *and* wanted output.
>
> I don't understand why, but I've been seeing repeating messages on
> several usenet groups this week. While it could be user error, I am
> beginning to suspect some sort of tech issue.

I only hit the send once!!
From: Alexandre Ferrieux on
On Mar 18, 4:12 pm, Cesear <ces...(a)gmail.com> wrote:
> On Mar 18, 10:32 am, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
> wrote:
>
> > On Mar 18, 1:52 pm, Cesear <ces...(a)gmail.com> wrote:
>
> > > Arjen u code works well, but I need to modify it some.  I need to be
> > > able to join the final file and only show the unqiueid at the
> > > beginning of the row.  How can I do that?
>
> > Instead of repeating the question 4 times, just post the full example
> > with inputs *and* wanted output.
>
> > -Alex
>
> Sorry must have hit the reply too many times  :)  Ok here is what I
> want, just data no English :) --->>>
>
> File ONE-->
>
> V0100|NAME1|blah|blah|
> V0102|NAME2|blah|blah|
>
> File TWO-->
>
> V0100|NAME1|chargeX|blah|
> V0100|NAME1|chargeY|blah|
> V0102|NAME2|chargeX|blah|
> V0102|NAME2|chargeY|blah|
> V0100|NAME1|blahcharge|blah|
> V0102|NAME2|blahcahrge|blah|
>
> The FINAL OUTPUT, I want to look like this-->
>
> V0100|NAME1|blah|blah|chargeX|blah|
> V0100|NAME1|blah|blah|chargeY|blah|
> V0102|NAME2|blah|blah|chargeX|blah|
> V0102|NAME2|blah|blah|chargeY|blah|
> V0100|NAME1|blah|blah|blahcharge|blah|
> V0102|NAME2|blah|blah|blahcahrge|blah|

Ah, what you're after is called a "join" in database circles.
It is roughly a "diagonal" hyperplane in the cartesian product of the
input.

Now, to compute it, you can first use simple unix commands: 'sort' and
'join'.
If you accept the result to be sorted on the joined field (2nd field
in your example):

sort -t \| -k 2,2 FILE1 > tmp1
sort -t \| -k 2,2 FILE2 > tmp2
join -t \| -j 2 tmp1 tmp2 > tmp3

note that the fields are not exactly in the order you want. To get
them right:

awk -F \| '{print $2,$1,$3,$4,$7,$8}' OFS=\| < tmp3 > OUTPUT


Now, since this is comp.lang.tcl, you can also do it in Tcl of
course ;-)
The idea is to build an internal "lookup table" based on FILE1, keyed
on 2nd field:

set ff [open FILE1 r]
while {[gets $ff line]>=0} {
set key [lindex [split $line |] 1]
set tab($key) $line
}
close $ff

Then you just scan the remaining inputs, and concatenate each line
with the lookup result:

set ff [open FILE2 r]
while {[gets $ff line]>=0} {
set key [lindex [split $line |] 1]
puts [join [concat [split $tab($key) |] [lrange [split $line |] 2
end]] |]
}

-Alex