merge two files

merge two files

am 19.11.2007 19:16:21 von onkelheinz

Hi,

there are two files:

FILE_X:
A
1
2
3

B
1
4

C
2
3

FILE_Y
1
2
3
4
5
6

What I need is:

1;A;B
2;A;C
3;A;C
4;B
5;
6;

The order should be like in FILE_Y. FILE_X must not include all
numbers ( better: characters ) that are listed in FILE_Y ( see 5 and 6 )
Please pay attention to the space between the blocks in FILE_X!

I'm not so familiar with awk or sed coding that.

Thanks!

Regards!

Heinz

Re: merge two files

am 19.11.2007 20:07:18 von Janis Papanagnou

Heinz Müller wrote:
> Hi,
>
> there are two files:
>
> FILE_X:
> A
> 1
> 2
> 3
>
> B
> 1
> 4
>
> C
> 2
> 3
>
> FILE_Y
> 1
> 2
> 3
> 4
> 5
> 6
>
> What I need is:
>
> 1;A;B
> 2;A;C
> 3;A;C
> 4;B
> 5;
> 6;
>
> The order should be like in FILE_Y. FILE_X must not include all
> numbers ( better: characters ) that are listed in FILE_Y ( see 5 and 6 )
> Please pay attention to the space between the blocks in FILE_X!

Try this...

awk '
BEGIN {RS="";FS="\n"}
NR==FNR {z=split($0,o);next}
{ for(n=2;n<=NF;n++) a[$n]=a[$n]";"$1}
END { for(i=1;i<=z;i++) print o[i] (a[o[i]]?"":";") a[o[i]] }
' FILE_Y FILE_X


Janis

>
> I'm not so familiar with awk or sed coding that.
>
> Thanks!
>
> Regards!
>
> Heinz
>
>

Re: merge two files

am 19.11.2007 20:30:12 von onkelheinz

"Janis Papanagnou" schrieb im Newsbeitrag
news:fhsmt6$bu1$1@online.de...
> Try this...
>
> awk '
> BEGIN {RS="";FS="\n"}
> NR==FNR {z=split($0,o);next}
> { for(n=2;n<=NF;n++) a[$n]=a[$n]";"$1}
> END { for(i=1;i<=z;i++) print o[i] (a[o[i]]?"":";") a[o[i]] }
> ' FILE_Y FILE_X
>
>
> Janis

Muhahahaha!! Works like a charm :-))))))

Changed awk with nawk ( SOLARIS 10 )!

How long did you code for that?

Thanks a lot!!

Heinz

Re: merge two files

am 19.11.2007 21:26:58 von Janis Papanagnou

Heinz Müller wrote:
> "Janis Papanagnou" schrieb im Newsbeitrag
> news:fhsmt6$bu1$1@online.de...
>
>>Try this...
>>
>> awk '
>> BEGIN {RS="";FS="\n"}
>> NR==FNR {z=split($0,o);next}
>> { for(n=2;n<=NF;n++) a[$n]=a[$n]";"$1}
>> END { for(i=1;i<=z;i++) print o[i] (a[o[i]]?"":";") a[o[i]] }
>> ' FILE_Y FILE_X
>>
>>
>>Janis
>
>
> Muhahahaha!! Works like a charm :-))))))
>
> Changed awk with nawk ( SOLARIS 10 )!
>
> How long did you code for that?

Apparently less than the difference of the timestamps of our two
postings; frankly, I don't know, maybe 15 minutes, maybe less.
The only "problem" that bugged me a bit, but more because of
aesthetical reasons, was the introduction of the special handling
(a[o[i]]?"":";") which I initially intended to somehow avoid.

Janis

>
> Thanks a lot!!
>
> Heinz
>
>