Joindre deux fichier avec deux champs de jointure

Hello,

I come to you asking you a hand on a script that I have performed on a unix server.

I have two files that I have to concatenate in a single line.

The first file is created like this:

mag;code_art;campagne;st_juillet;st_aout;etc

The second file is created like this:

code_art;campagne;pma_juillet;pma_aout;etc

and the final should be like this:

mag;code_art;campagne;st_juillet;st_aout;etc;pma_juillet;pma_aout;etc...

Basically I do a join on two fields that are code_art and campagne.

And I plant, because I tried to join but the result is false because the joint can be achieved only on a field.
Basically, I give you an example and hope that you will be inspired much more than me because I really dry.

Example:

The first file includes this:

mag;code_art;campagne;st_juillet;st_aout;etc
201;106;2007;12;23;etc
201;206;2008;11;13;etc
113;206;2007;12;14;etc
112;208;2008;34;25;etc

The second file contains this:

code_art;campagne;pma_juillet;pma_aout;etc
206;2007;112;118,etc
201;2008;115;120;etc
206;2008;175;195;etc
106;2007;340;152;etc
208;2008;32;12;etc
etc.........

and the final file should look like this:

mag;code_art;campagne;st_juillet;st_aout;etc;pma_juillet;pma*_aout;etc
201;206;2008;11;13;etc;112;118;etc
201;106;2007;12;23;etc;340;152;etc
112;208;2008;34;25;etc;32;12;etc

I finish this post a little long, thank you in advance for your help.

Amicalement
Steph70

English please. These forums are English-language.

En Anglais, s'il vous plait. Please post in English.

Per our forum rules, all posts must be in English.

We do provide translation services for posts from English to a number of languages as a benefit to users. However, posts must be in English.

Please repost in English.

Thank you for your cooperation.

The UNIX and Linux Forums.

EDIT: after re-reading your post, this script does not correspong to what you want to do ... but it can help you
---------------
I came up with a script -in bash- like this, I hope it will be useful:

#!/bin/bash

array1=($(sed 's/;/\ /g' file1))
array2=($(sed 's/;/\ /g' file2))
cat /dev/null > file3
for ((i=0; i<${#array1
[*]}; i++)); do
    echo ${array1[$i]}';'${array2[$i]} >> file3;
done
exit 0

file1 and file2 are text files containing your datas
file3 will be created with your data joined together
How does it works:
Lines 3 and 4: we create 2 arrays containing datas (we replace ; with \ (spaces))
Line 5; we create a empty file3
Line 6, 7 and 8: we join together datas from array1 and array 2.
The script was tested with 2 sample files:

file1:
1;3;5;7;9
11;13;15;17;19

file2;
2;4;6;8;10
12;14;16;18;20

resulting file3:
1;2
3;4
5;6
7;8
9;10
11;12
13;14
15;16
17;18
19;20

Excuse me for the language.
I went a little fast.

I do care now.

Thank you tukuyomi. I look and I tested.

#!/bin/bash

array1=($(sed 's/;/\ /g' file1))
array2=($(sed 's/;/\ /g' file2))
cat /dev/null > file3

i=0; j=0
while read line; do
    echo ${array1[(($i+0))]}';'${array1[(($i+1))]}';'${array1[(($i+2))]}';'${array1[(($i+3))]}';'${array1[(($i+4))]}';'${array1[(($i+5))]}';'${array2[(($j+2))]}';'${array2[(($j+3))]}';'${array2[(($j+4))]} >> file3
    ((i+=6)); ((j+=5))
done < file1
exit 0

This one will work, BUT
file1 AND file2 must have the same exact number of lines
file1 has exactly 6 fields delimited with ; ((i+=6))
file2 has exactly 5 fields delimited with ; ((j+=5))

By the way, you have a comma (,) instead of a semi-colon (:wink: in your file2 sample (2nd line); it took me about an hour to find it :smiley:

Thank you again

The concern is that both files have not precisely the same number of line.

The first should be about 400 000 and the second about half.

It is not easy

I suggest you then to put the name of the smallest file on the line near the end of the script:

    ((i+=6)); ((j+=5))
done < smallestfile
exit 0