Discussion:
join two files
MOKRANI Rachid
2013-05-15 11:55:47 UTC
Permalink
Hi,

I need some help for doing the following command with sed. Is someone
could gibe me the exemple ?

Many thanks.


=============================
cat /tmp/file_1

Num;Nbr_pass;
5;1;
7;1;
8;4;
12;1;
13;5;
15;7;
=============================

cat /tmp/file2

Num ; Comments ;
1 ; AAAA ;
2 ; BBBBB ;
3 ; CC ;
4 ; XXDF ;
5 ; KIJHG ;
6 ; GFRD ;
7 ; HGTFEDFR ;
8 ; DJHY ;
9 ; FDGRTFG ;
10 ; QWXVG ;
11 ; FTGJ ;
12 ; DHGRF ;
13 ; GFDRFE ;
14 ; GFDRFE ;
15 ; FGTF ;
16 ; LKJU ;
17 ; DFDE ;

=============================
The result, I would like to have.


Num ; Comments ; Nbr_pass ;
1 ; AAAA ; N/A ;
2 ; BBBBB ; N/A;
3 ; CC ; N/A;
4 ; XXDF ; N/A;
5 ; KIJHG ;1 ;
6 ; GFRD ; N/A;
7 ; HGTFEDFR ; 1 ;
8 ; DJHY ; 1 ;
9 ; FDGRTFG ; N/A;
10 ; QWXVG ; N/A;
11 ; FTGJ ; N/A;
12 ; DHGRF ; 1 ;
13 ; GFDRFE ; 5 ;
14 ; GFDRFE ; N/A;
15 ; FGTF ; 7 ;
16 ; LKJU ; N/A;
17 ; DFDE ; N/A;



Many thanks for help.
Regards.


__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________


[Non-text portions of this message have been removed]
Thierry Blanc
2013-05-15 12:23:27 UTC
Permalink
sed "$(sed 's|;|s/$/|;s|;| ;/|' file1)" file2 |sed '/;.*;.*;.*/!s|$|
N/A;|'

probably it is possible to integrate the last sed statement into the
first ...
Post by MOKRANI Rachid
Hi,
I need some help for doing the following command with sed. Is someone
could gibe me the exemple ?
Many thanks.
=============================
cat /tmp/file_1
Num;Nbr_pass;
5;1;
7;1;
8;4;
12;1;
13;5;
15;7;
=============================
cat /tmp/file2
Num ; Comments ;
1 ; AAAA ;
2 ; BBBBB ;
3 ; CC ;
4 ; XXDF ;
5 ; KIJHG ;
6 ; GFRD ;
7 ; HGTFEDFR ;
8 ; DJHY ;
9 ; FDGRTFG ;
10 ; QWXVG ;
11 ; FTGJ ;
12 ; DHGRF ;
13 ; GFDRFE ;
14 ; GFDRFE ;
15 ; FGTF ;
16 ; LKJU ;
17 ; DFDE ;
=============================
The result, I would like to have.
Num ; Comments ; Nbr_pass ;
1 ; AAAA ; N/A ;
2 ; BBBBB ; N/A;
3 ; CC ; N/A;
4 ; XXDF ; N/A;
5 ; KIJHG ;1 ;
6 ; GFRD ; N/A;
7 ; HGTFEDFR ; 1 ;
8 ; DJHY ; 1 ;
9 ; FDGRTFG ; N/A;
10 ; QWXVG ; N/A;
11 ; FTGJ ; N/A;
12 ; DHGRF ; 1 ;
13 ; GFDRFE ; 5 ;
14 ; GFDRFE ; N/A;
15 ; FGTF ; 7 ;
16 ; LKJU ; N/A;
17 ; DFDE ; N/A;
Many thanks for help.
Regards.
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
[Non-text portions of this message have been removed]
------------------------------------
MOKRANI Rachid
2013-05-15 13:26:42 UTC
Permalink
Thanks Thierry,

But I think we don't have the same sed version.

My release is (standard CentOS):
sed --version
GNU sed version 4.2.1


Doesn't work.
-----Message d'origine-----
Envoyé : mercredi 15 mai 2013 14:23
À : MOKRANI Rachid
Objet : Re: join two files
sed "$(sed 's|;|s/$/|;s|;| ;/|' file1)" file2 |sed
'/;.*;.*;.*/!s|$|
N/A;|'
probably it is possible to integrate the last sed statement into the
first ...
Post by MOKRANI Rachid
Hi,
I need some help for doing the following command with sed.
Is someone
Post by MOKRANI Rachid
could gibe me the exemple ?
Many thanks.
=============================
cat /tmp/file_1
Num;Nbr_pass;
5;1;
7;1;
8;4;
12;1;
13;5;
15;7;
=============================
cat /tmp/file2
Num ; Comments ;
1 ; AAAA ;
2 ; BBBBB ;
3 ; CC ;
4 ; XXDF ;
5 ; KIJHG ;
6 ; GFRD ;
7 ; HGTFEDFR ;
8 ; DJHY ;
9 ; FDGRTFG ;
10 ; QWXVG ;
11 ; FTGJ ;
12 ; DHGRF ;
13 ; GFDRFE ;
14 ; GFDRFE ;
15 ; FGTF ;
16 ; LKJU ;
17 ; DFDE ;
=============================
The result, I would like to have.
Num ; Comments ; Nbr_pass ;
1 ; AAAA ; N/A ;
2 ; BBBBB ; N/A;
3 ; CC ; N/A;
4 ; XXDF ; N/A;
5 ; KIJHG ;1 ;
6 ; GFRD ; N/A;
7 ; HGTFEDFR ; 1 ;
8 ; DJHY ; 1 ;
9 ; FDGRTFG ; N/A;
10 ; QWXVG ; N/A;
11 ; FTGJ ; N/A;
12 ; DHGRF ; 1 ;
13 ; GFDRFE ; 5 ;
14 ; GFDRFE ; N/A;
15 ; FGTF ; 7 ;
16 ; LKJU ; N/A;
17 ; DFDE ; N/A;
Many thanks for help.
Regards.
__________________________
Avant d'imprimer, pensez à l'environnement ! Please
consider the environment before printing !
Post by MOKRANI Rachid
Ce message et toutes ses pièces jointes sont confidentiels
et établis à l'intention exclusive de ses destinataires.
Toute utilisation non conforme à sa destination, toute
diffusion ou toute publication, totale ou partielle, est
interdite, sauf autorisation expresse. IFP Energies nouvelles
décline toute responsabilité au titre de ce message. This
message and any attachments are confidential and intended
solely for the addressees. Any unauthorised use or
dissemination is prohibited. IFP Energies nouvelles should
not be liable for this message.
Post by MOKRANI Rachid
__________________________
[Non-text portions of this message have been removed]
------------------------------------
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses pièces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
Amarendra Godbole
2013-05-15 13:47:59 UTC
Permalink
Rachid,

Thierry may not have tested his example mostly, and hence sed version should not matter. With some tweaks, and knowledge of sed, you should be able to get it work. Thanks.

-ag
--
sent via 100% recycled electrons from my mobile command center.
Post by MOKRANI Rachid
Thanks Thierry,
But I think we don't have the same sed version.
sed --version
GNU sed version 4.2.1
Doesn't work.
-----Message d'origine-----
Envoyé : mercredi 15 mai 2013 14:23
À : MOKRANI Rachid
Objet : Re: join two files
sed "$(sed 's|;|s/$/|;s|;| ;/|' file1)" file2 |sed
'/;.*;.*;.*/!s|$|
N/A;|'
probably it is possible to integrate the last sed statement into the
first ...
Post by MOKRANI Rachid
Hi,
I need some help for doing the following command with sed.
Is someone
Post by MOKRANI Rachid
could gibe me the exemple ?
Many thanks.
=============================
cat /tmp/file_1
Num;Nbr_pass;
5;1;
7;1;
8;4;
12;1;
13;5;
15;7;
=============================
cat /tmp/file2
Num ; Comments ;
1 ; AAAA ;
2 ; BBBBB ;
3 ; CC ;
4 ; XXDF ;
5 ; KIJHG ;
6 ; GFRD ;
7 ; HGTFEDFR ;
8 ; DJHY ;
9 ; FDGRTFG ;
10 ; QWXVG ;
11 ; FTGJ ;
12 ; DHGRF ;
13 ; GFDRFE ;
14 ; GFDRFE ;
15 ; FGTF ;
16 ; LKJU ;
17 ; DFDE ;
=============================
The result, I would like to have.
Num ; Comments ; Nbr_pass ;
1 ; AAAA ; N/A ;
2 ; BBBBB ; N/A;
3 ; CC ; N/A;
4 ; XXDF ; N/A;
5 ; KIJHG ;1 ;
6 ; GFRD ; N/A;
7 ; HGTFEDFR ; 1 ;
8 ; DJHY ; 1 ;
9 ; FDGRTFG ; N/A;
10 ; QWXVG ; N/A;
11 ; FTGJ ; N/A;
12 ; DHGRF ; 1 ;
13 ; GFDRFE ; 5 ;
14 ; GFDRFE ; N/A;
15 ; FGTF ; 7 ;
16 ; LKJU ; N/A;
17 ; DFDE ; N/A;
Many thanks for help.
Regards.
__________________________
Avant d'imprimer, pensez à l'environnement ! Please
consider the environment before printing !
Post by MOKRANI Rachid
Ce message et toutes ses piÚces jointes sont confidentiels
et établis à l'intention exclusive de ses destinataires.
Toute utilisation non conforme à sa destination, toute
diffusion ou toute publication, totale ou partielle, est
interdite, sauf autorisation expresse. IFP Energies nouvelles
décline toute responsabilité au titre de ce message. This
message and any attachments are confidential and intended
solely for the addressees. Any unauthorised use or
dissemination is prohibited. IFP Energies nouvelles should
not be liable for this message.
Post by MOKRANI Rachid
__________________________
[Non-text portions of this message have been removed]
------------------------------------
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
------------------------------------
--
Yahoo! Groups Links
Thierry Blanc
2013-05-15 14:10:50 UTC
Permalink
GNU sed version 4.2.1

well, I tested it, it worked on ubuntu 12.10, kernel 3.7...


$ sed "$(sed 's|;|s/$/|;s|;| ;/|' file1)" file2 |sed '/;.*;.*;.*/!s|$|
N/A;|'
1 ; AAAA ; N/A;
2 ; BBBBB ; N/A;
3 ; CC ; N/A;
4 ; XXDF ; N/A;
5 ; KIJHG ;1 ;
6 ; GFRD ; N/A;
7 ; HGTFEDFR ;1 ;
8 ; DJHY ;4 ;
9 ; FDGRTFG ; N/A;
10 ; QWXVG ; N/A;
11 ; FTGJ ; N/A;
12 ; DHGRF ;1 ;
13 ; GFDRFE ;5 ;
14 ; GFDRFE ; N/A;
15 ; FGTF ;7 ;
16 ; LKJU ; N/A;
17 ; DFDE ; N/A;
Post by Amarendra Godbole
Rachid,
Thierry may not have tested his example mostly, and hence sed version should not matter. With some tweaks, and knowledge of sed, you should be able to get it work. Thanks.
-ag
--
sent via 100% recycled electrons from my mobile command center.
Post by MOKRANI Rachid
Thanks Thierry,
But I think we don't have the same sed version.
sed --version
GNU sed version 4.2.1
Doesn't work.
-----Message d'origine-----
Envoyé : mercredi 15 mai 2013 14:23
À : MOKRANI Rachid
Objet : Re: join two files
sed "$(sed 's|;|s/$/|;s|;| ;/|' file1)" file2 |sed
'/;.*;.*;.*/!s|$|
N/A;|'
probably it is possible to integrate the last sed statement into the
first ...
Post by MOKRANI Rachid
Hi,
I need some help for doing the following command with sed.
Is someone
Post by MOKRANI Rachid
could gibe me the exemple ?
Many thanks.
=============================
cat /tmp/file_1
Num;Nbr_pass;
5;1;
7;1;
8;4;
12;1;
13;5;
15;7;
=============================
cat /tmp/file2
Num ; Comments ;
1 ; AAAA ;
2 ; BBBBB ;
3 ; CC ;
4 ; XXDF ;
5 ; KIJHG ;
6 ; GFRD ;
7 ; HGTFEDFR ;
8 ; DJHY ;
9 ; FDGRTFG ;
10 ; QWXVG ;
11 ; FTGJ ;
12 ; DHGRF ;
13 ; GFDRFE ;
14 ; GFDRFE ;
15 ; FGTF ;
16 ; LKJU ;
17 ; DFDE ;
=============================
The result, I would like to have.
Num ; Comments ; Nbr_pass ;
1 ; AAAA ; N/A ;
2 ; BBBBB ; N/A;
3 ; CC ; N/A;
4 ; XXDF ; N/A;
5 ; KIJHG ;1 ;
6 ; GFRD ; N/A;
7 ; HGTFEDFR ; 1 ;
8 ; DJHY ; 1 ;
9 ; FDGRTFG ; N/A;
10 ; QWXVG ; N/A;
11 ; FTGJ ; N/A;
12 ; DHGRF ; 1 ;
13 ; GFDRFE ; 5 ;
14 ; GFDRFE ; N/A;
15 ; FGTF ; 7 ;
16 ; LKJU ; N/A;
17 ; DFDE ; N/A;
Many thanks for help.
Regards.
__________________________
Avant d'imprimer, pensez à l'environnement ! Please
consider the environment before printing !
Post by MOKRANI Rachid
Ce message et toutes ses piÚces jointes sont confidentiels
et établis à l'intention exclusive de ses destinataires.
Toute utilisation non conforme à sa destination, toute
diffusion ou toute publication, totale ou partielle, est
interdite, sauf autorisation expresse. IFP Energies nouvelles
décline toute responsabilité au titre de ce message. This
message and any attachments are confidential and intended
solely for the addressees. Any unauthorised use or
dissemination is prohibited. IFP Energies nouvelles should
not be liable for this message.
Post by MOKRANI Rachid
__________________________
[Non-text portions of this message have been removed]
------------------------------------
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
------------------------------------
--
Yahoo! Groups Links
------------------------------------
Jim Hill
2013-05-15 15:23:23 UTC
Permalink
I'd use `join` to get the data itself in the right places and `sed` to do
the formatting.


[Non-text portions of this message have been removed]
MOKRANI Rachid
2013-05-15 16:19:28 UTC
Permalink
well, join don't give the good result :-(
and same with paste, no good result.

/usr/bin/join --nocheck-order -t ';' -a 1 file1 file2
Num;Nbr_pass;;Comments;
5;1;;KIJHG;
7;1;;HGTFEDFR;
8;4;;DJHY;
12;1;
13;1;
15;7;


A sed syntax would be appreciated. (CentOS 6.3 - kernel 2.6.32 - sed release 4.2.1)

Thanks in advance.
-----Message d'origine-----
Envoyé : mercredi 15 mai 2013 17:23
Objet : Re: join two files
I'd use `join` to get the data itself in the right places and
`sed` to do
the formatting.
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses pièces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
Frost
2013-05-16 02:21:30 UTC
Permalink
Hello,

Try:

catEndingFile.txt >> BeginningFile.txt

The >> will append the first file to the end of the second file.
You can also:

cat file1.txt file2.txt file3.txt > file123.txt

Michael
Werfgam Nadler
2013-05-16 10:47:44 UTC
Permalink
Hello,

here is a solution.

sed  '
$!{
N;
s/^/\n/;
D
}

/^\([^\n; ]*\)\( *\);.*\n\1\( *\);/!{

s/\n/ N\/A ;&/
/\n/!s/$/ N\/A;/
}

/^\([^\n; ]*\)\( *\);\([^\n][^\n]*\)\(.*\)\n\1\( *\);\([^\n][^\n]*\)/{

s//\1\2;\3 \6\5\4/;
}
P;
D;
'       file2    file1  
#  PS : it is ur file2 and file1 in that order.

this gives the solution below.




Num ; Comments ; Nbr_pass;
1 ; AAAA ; N/A ;
2 ; BBBBB ; N/A ;
3 ; CC ; N/A ;
4 ; XXDF ; N/A ;
5 ; KIJHG ; 1;
6 ; GFRD ; N/A ;
7 ; HGTFEDFR ; 1;
8 ; DJHY ; 4;
9 ; FDGRTFG ; N/A ;
10 ; QWXVG ; N/A ;
11 ; FTGJ ; N/A ;
12 ; DHGRF ; 1;
13 ; GFDRFE ; 5;
14 ; GFDRFE ; N/A ;
15 ; FGTF ; 7;
16 ; LKJU ; N/A ;
17 ; DFDE ; N/A;






________________________________
From: MOKRANI Rachid <***@ifpen.fr>
To: sed-***@yahoogroups.com
Sent: Wednesday, May 15, 2013 5:25 PM
Subject: join two files


Hi,

I need some help for doing the following command with sed. Is someone
could gibe me the exemple ?

Many thanks.


=============================
cat /tmp/file_1

Num;Nbr_pass;
5;1;
7;1;
8;4;
12;1;
13;5;
15;7;
=============================

cat /tmp/file2

Num ; Comments ;
1 ; AAAA ;
2 ; BBBBB ;
3 ; CC ;
4 ; XXDF ;
5 ; KIJHG ;
6 ; GFRD ;
7 ; HGTFEDFR ;
8 ; DJHY ;
9 ; FDGRTFG ;
10 ; QWXVG ;
11 ; FTGJ ;
12 ; DHGRF ;
13 ; GFDRFE ;
14 ; GFDRFE ;
15 ; FGTF ;
16 ; LKJU ;
17 ; DFDE ;

=============================
The result, I would like to have.


Num ; Comments ; Nbr_pass ;
1 ; AAAA ; N/A ;
2 ; BBBBB ; N/A;
3 ; CC ; N/A;
4 ; XXDF ; N/A;
5 ; KIJHG ;1 ;
6 ; GFRD ; N/A;
7 ; HGTFEDFR ; 1 ;
8 ; DJHY ; 1 ;
9 ; FDGRTFG ; N/A;
10 ; QWXVG ; N/A;
11 ; FTGJ ; N/A;
12 ; DHGRF ; 1 ;
13 ; GFDRFE ; 5 ;
14 ; GFDRFE ; N/A;
15 ; FGTF ; 7 ;
16 ; LKJU ; N/A;
17 ; DFDE ; N/A;



Many thanks for help.
Regards.


__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses pièces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________


[Non-text portions of this message have been removed]



------------------------------------
--
Yahoo! Groups Links



[Non-text portions of this message have been removed]
Ruud H.G. van Tol
2013-05-16 12:08:00 UTC
Permalink
In Perl:

perl -wpe '
BEGIN{%h=map{split/\s*;\s*/}`cat ./file1`}
s/((\S+).*)/"$1 ".($h{$2}||"N\/A")/e
' ./file2

Num ; Comments ; Nbr_pass
1 ; AAAA ; N/A
2 ; BBBBB ; N/A
3 ; CC ; N/A
4 ; XXDF ; N/A
5 ; KIJHG ; 1
6 ; GFRD ; N/A
7 ; HGTFEDFR ; 1
8 ; DJHY ; 4
9 ; FDGRTFG ; N/A
10 ; QWXVG ; N/A
11 ; FTGJ ; N/A
12 ; DHGRF ; 1
13 ; GFDRFE ; 5
14 ; GFDRFE ; N/A
15 ; FGTF ; 7
16 ; LKJU ; N/A
17 ; DFDE ; N/A


Make it:

/"$1 ".($h{$2}||"N\/A")." ;"/

if you need the trailing " ;".
--
Ruud
Ruud H.G. van Tol
2013-05-16 12:26:11 UTC
Permalink
Post by Ruud H.G. van Tol
perl -wpe '
BEGIN{%h=map{split/\s*;\s*/}`cat ./file1`}
s/((\S+).*)/"$1 ".($h{$2}||"N\/A")/e
' ./file2
Alternatives:

perl -wpe '
BEGIN{%h=`cat ./file1`=~/(\w+)\W+(\w+)/g}
s/((\S+).*)/"$1 ".($h{$2}||"N\/A")/e
' ./file2

perl -wpe '
BEGIN{%h=`cat ./file1`=~/(.*?);(.*)/g}
s/((\S+).*)/"$1 ".($h{$2}||"N\/A")/e
' ./file2
--
Ruud
Ruud H.G. van Tol
2013-05-16 20:15:42 UTC
Permalink
With autosplit:

perl -awlne'
BEGIN{%h=`cat ./file1`=~/(.*?);(.*)/g}
print"$_ ",$h{$F[0]}||"N\/A"
' ./file2

The BEGIN-block parses file1 into a hash. The print-line postfixes each
row from file2 with the extra info.

See `perldoc perlrun` about autosplit.
--
Ruud
Jim Hill
2013-05-16 15:57:40 UTC
Permalink
On 05/15/2013 11:51 PM, MOKRANI Rachid wrote:> Hi,
Well this is a good solution and it work. You should send it to the
list so that others can benefit too.
ok, happy to.

awk -vFS=\; -vOFS=\; '{ printf("%04d;%s\n",$1,$0) }' file1>@file1
awk -vFS=\; -vOFS=\; '{ printf("%04d;%s\n",$1,$0) }' file2>@file2
join -a1 -t\; -o 1.2,1.3,2.3 -eN/A @file2 @file1


So

- Therry Blanc's sed solution actually delivers what was requested, but
I have to wonder if the O(NM) wouldn't start mattering even on today's
hardware.

- Ruud H.G. van Tol's perl ... kinda shows perl's strengths, don't it?
and it's O(lgN M),

- and this one's linear and probably easier on the uninitiated, but
you'd have to tweak it to avoid multiple passes.

The O()'s are casual estimates, and unless this is for really
industrial-strength data volumes or overeager optimizers like me I can't
see them mattering much.
Ruud H.G. van Tol
2013-05-16 16:04:48 UTC
Permalink
Post by Jim Hill
- Ruud H.G. van Tol's perl ... kinda shows perl's strengths, don't it?
and it's O(lgN M),
The Perl variants only reads each file's line exactly once, so is as
optimal as it can get.
--
Ruud
Jim Hill
2013-05-16 16:25:08 UTC
Permalink
Post by Ruud H.G. van Tol
The Perl variants only reads each file's line exactly once, so is as
optimal as it can get.
I brain-farted on each key lookup being against the entire selection
set, of course it's a hash lookup. Apologies, I guess I have to correct
"casual" to "hasty".
Davide Brini
2013-05-16 16:36:52 UTC
Permalink
Post by Jim Hill
On 05/15/2013 11:51 PM, MOKRANI Rachid wrote:> Hi,
Well this is a good solution and it work. You should send it to the
list so that others can benefit too.
ok, happy to.
Why not the simpler and single-pass

awk -F ' *; *' '

NR == FNR {
a[$1] = $2
next
}

{
f = ( ($1 in a) ? a[$1] : "N/A" )
print $0 " " f ";"
}

' file1 file2


I assume spacing around semicolons isn't relevant; in case it is, the
modifications are obvious (although I'd consider such an environment
slightly broken).
--
D.
Jim Hill
2013-05-16 16:52:10 UTC
Permalink
Post by Davide Brini
Why not the simpler and single-pass
I was in old-school mode, avoiding holding the entire search set in
memory. I can't imagine any plausible circumstance where that could be
a problem these days.
Loading...