Discussion:
manipulate column
MOKRANI Rachid
2013-09-27 08:35:40 UTC
Permalink
Hi,



I have two files. (with more than 5000 lines)



The first file A.txt,



Name_1; Firstname_1; address_A; phone_number;

Name_2; Firstname_2; address_B; phone_number;

Name_30; Firstname_3; address_C; phone_number;

Name_45; Firstname_4; address_D; phone_number;

Name_50; Firstname_5; address_E; phone_number;

Name_60; Firstname_5; address_E; phone_number;





The second file B.txt



0001;Name_1; Firstname_1; address_A; phone_number;

0002;Name_2; Firstname_2; address_B; phone_number;

0003; Name_3;Firstname_3; address_C; phone_number;

1111;Name_4; Firstname_4; address_D; phone_number;

5555;Name_5; Firstname_5; address_E; phone_number;

4567;Name_6; Firstname_6; address_F; phone_number;

1256;Name_7; Firstname_7; address_G; phone_number;

4953;Name_8; Firstname_8; address_H; phone_number;

7896;Name_60; Firstname_5; address_I; phone_number;

7897;Name_61; Firstname_61; address_J; phone_number;







I would like to have :



0001;Name_1; Firstname_1; address_A; phone_number;

0002;Name_2; Firstname_2; address_B; phone_number;

XXXX;Name_30; Firstname_3; address_C; phone_number;

XXXX;Name_45; Firstname_4; address_D; phone_number;

XXXX;Name_50; Firstname_5; address_E; phone_number;

7896;Name_60; Firstname_5; address_E; phone_number;







Any help would be appreciated.

Best 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.
__________________________
Thierry Blanc
2013-09-27 11:24:33 UTC
Permalink
similar problem that you already asked some month ago.

Am I correct that you do not want to have the entries that are only in
file 2


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



/^([^;]*);.*\n([^;]*);\1/!{
/^[0-9][0-9]*;/b

s|^|&XXX;|
#/\n/!s/$/ N\/A;/

}

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

s||\3;\1\2|;
}
P;
#/^[0-9]/q
D;
Post by MOKRANI Rachid
Hi,
I have two files. (with more than 5000 lines)
The first file A.txt,
Name_1; Firstname_1; address_A; phone_number;
Name_2; Firstname_2; address_B; phone_number;
Name_30; Firstname_3; address_C; phone_number;
Name_45; Firstname_4; address_D; phone_number;
Name_50; Firstname_5; address_E; phone_number;
Name_60; Firstname_5; address_E; phone_number;
The second file B.txt
0001;Name_1; Firstname_1; address_A; phone_number;
0002;Name_2; Firstname_2; address_B; phone_number;
0003; Name_3;Firstname_3; address_C; phone_number;
1111;Name_4; Firstname_4; address_D; phone_number;
5555;Name_5; Firstname_5; address_E; phone_number;
4567;Name_6; Firstname_6; address_F; phone_number;
1256;Name_7; Firstname_7; address_G; phone_number;
4953;Name_8; Firstname_8; address_H; phone_number;
7896;Name_60; Firstname_5; address_I; phone_number;
7897;Name_61; Firstname_61; address_J; phone_number;
0001;Name_1; Firstname_1; address_A; phone_number;
0002;Name_2; Firstname_2; address_B; phone_number;
XXXX;Name_30; Firstname_3; address_C; phone_number;
XXXX;Name_45; Firstname_4; address_D; phone_number;
XXXX;Name_50; Firstname_5; address_E; phone_number;
7896;Name_60; Firstname_5; address_E; phone_number;
Any help would be appreciated.
Best 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./
__________________________
MOKRANI Rachid
2013-09-27 12:04:37 UTC
Permalink
Hi,



In the result, it will be the same content as the A.txt file with a new column at the beginning of the line.

This new column will take a number in the file B.txt if it exists for the same name.


And if the name in file A.txt does not exist in the file B.txt added XXX instead beginning of the line in the result .





De : sed-***@yahoogroups.com [mailto:sed-***@yahoogroups.com] De la part de Thierry Blanc
Envoyé : vendredi 27 septembre 2013 13:25
À : MOKRANI Rachid
Cc : sed-***@yahoogroups.com
Objet : Re: manipulate column









similar problem that you already asked some month ago.

Am I correct that you do not want to have the entries that are only in file 2


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



/^([^;]*);.*\n([^;]*);\1/!{
/^[0-9][0-9]*;/b

s|^|&XXX;|
#/\n/!s/$/ N\/A;/

}

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

s||\3;\1\2|;
}
P;
#/^[0-9]/q
D;




On 27/09/13 14:05, MOKRANI Rachid wrote:

Hi,



I have two files. (with more than 5000 lines)



The first file A.txt,



Name_1; Firstname_1; address_A; phone_number;

Name_2; Firstname_2; address_B; phone_number;

Name_30; Firstname_3; address_C; phone_number;

Name_45; Firstname_4; address_D; phone_number;

Name_50; Firstname_5; address_E; phone_number;

Name_60; Firstname_5; address_E; phone_number;





The second file B.txt



0001;Name_1; Firstname_1; address_A; phone_number;

0002;Name_2; Firstname_2; address_B; phone_number;

0003; Name_3;Firstname_3; address_C; phone_number;

1111;Name_4; Firstname_4; address_D; phone_number;

5555;Name_5; Firstname_5; address_E; phone_number;

4567;Name_6; Firstname_6; address_F; phone_number;

1256;Name_7; Firstname_7; address_G; phone_number;

4953;Name_8; Firstname_8; address_H; phone_number;

7896;Name_60; Firstname_5; address_I; phone_number;

7897;Name_61; Firstname_61; address_J; phone_number;







I would like to have :



0001;Name_1; Firstname_1; address_A; phone_number;

0002;Name_2; Firstname_2; address_B; phone_number;

XXXX;Name_30; Firstname_3; address_C; phone_number;

XXXX;Name_45; Firstname_4; address_D; phone_number;

XXXX;Name_50; Firstname_5; address_E; phone_number;

7896;Name_60; Firstname_5; address_E; phone_number;







Any help would be appreciated.

Best 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.
__________________________









__________________________
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.
__________________________
Davide Brini
2013-09-28 09:46:03 UTC
Permalink
On Fri, 27 Sep 2013 10:35:40 +0200, "MOKRANI Rachid"
Post by MOKRANI Rachid
Hi,
I have two files. (with more than 5000 lines)
The first file A.txt,
Name_1; Firstname_1; address_A; phone_number;
Name_2; Firstname_2; address_B; phone_number;
Name_30; Firstname_3; address_C; phone_number;
Name_45; Firstname_4; address_D; phone_number;
Name_50; Firstname_5; address_E; phone_number;
Name_60; Firstname_5; address_E; phone_number;
The second file B.txt
0001;Name_1; Firstname_1; address_A; phone_number;
0002;Name_2; Firstname_2; address_B; phone_number;
0003; Name_3;Firstname_3; address_C; phone_number;
1111;Name_4; Firstname_4; address_D; phone_number;
5555;Name_5; Firstname_5; address_E; phone_number;
4567;Name_6; Firstname_6; address_F; phone_number;
1256;Name_7; Firstname_7; address_G; phone_number;
4953;Name_8; Firstname_8; address_H; phone_number;
7896;Name_60; Firstname_5; address_I; phone_number;
7897;Name_61; Firstname_61; address_J; phone_number;
0001;Name_1; Firstname_1; address_A; phone_number;
0002;Name_2; Firstname_2; address_B; phone_number;
XXXX;Name_30; Firstname_3; address_C; phone_number;
XXXX;Name_45; Firstname_4; address_D; phone_number;
XXXX;Name_50; Firstname_5; address_E; phone_number;
7896;Name_60; Firstname_5; address_E; phone_number;
This is trivial with awk:

awk -F ';' '

NR==FNR { a[$2] = $1; next }
{ print ( ($1 in a) ? a[$1] : "XXXX" ) ";" $0 }

' fileB.txt fileA.txt

Not so much with sed, but if you insist on using it, you can do

sed 's|\([^;]*\);\([^;]*\);.*|s/^\2;/\1;\2;/;t|
$a\
s/^/XXXX;/' fileB.txt | sed -f- fileA.txt
--
D.
Thierry Blanc
2013-09-28 13:31:03 UTC
Permalink
the problems by Rachid are similar.

Maybe a bash solution -- awfully slow, of course -- but easy to
understand could help

while read IFS=';' A B C D E # read all column data into variables, IFS
is field separator
do
XXX="$(sed ...)" # extract the data you need from other file
if [[ -z "$XXX" ]]
then
echo "XXX";$A;$B;$C ....
else
echo $XXX;$A;$B;$C ....
fi
done < file1. txt
Post by Davide Brini
On Fri, 27 Sep 2013 10:35:40 +0200, "MOKRANI Rachid"
Post by MOKRANI Rachid
Hi,
I have two files. (with more than 5000 lines)
The first file A.txt,
Name_1; Firstname_1; address_A; phone_number;
Name_2; Firstname_2; address_B; phone_number;
Name_30; Firstname_3; address_C; phone_number;
Name_45; Firstname_4; address_D; phone_number;
Name_50; Firstname_5; address_E; phone_number;
Name_60; Firstname_5; address_E; phone_number;
The second file B.txt
0001;Name_1; Firstname_1; address_A; phone_number;
0002;Name_2; Firstname_2; address_B; phone_number;
0003; Name_3;Firstname_3; address_C; phone_number;
1111;Name_4; Firstname_4; address_D; phone_number;
5555;Name_5; Firstname_5; address_E; phone_number;
4567;Name_6; Firstname_6; address_F; phone_number;
1256;Name_7; Firstname_7; address_G; phone_number;
4953;Name_8; Firstname_8; address_H; phone_number;
7896;Name_60; Firstname_5; address_I; phone_number;
7897;Name_61; Firstname_61; address_J; phone_number;
0001;Name_1; Firstname_1; address_A; phone_number;
0002;Name_2; Firstname_2; address_B; phone_number;
XXXX;Name_30; Firstname_3; address_C; phone_number;
XXXX;Name_45; Firstname_4; address_D; phone_number;
XXXX;Name_50; Firstname_5; address_E; phone_number;
7896;Name_60; Firstname_5; address_E; phone_number;
awk -F ';' '
NR==FNR { a[$2] = $1; next }
{ print ( ($1 in a) ? a[$1] : "XXXX" ) ";" $0 }
' fileB.txt fileA.txt
Not so much with sed, but if you insist on using it, you can do
sed 's|\([^;]*\);\([^;]*\);.*|s/^\2;/\1;\2;/;t|
$a\
s/^/XXXX;/' fileB.txt | sed -f- fileA.txt
Loading...