Remplacement de chaînes de caractères - Un script peut-être?

Bonjour
Je dispose de deux fichiers textes:

  • le fichier 1 contient certaines lignes contenant (pas toutes) ce type de chaîne (les guillemets n’y sont pas):
    " ELSET=PSHELL 12345678,"
    " ELSET=PSHELL 12345678_1,"
    " ELSET=PSHELL 12345678_2,"
    " ELSET=PSHELL 88884855,"
    " ELSET=PSHELL 23425456,"
    " ELSET=PSHELL 54355454,"
    etc

  • le fichier 2 contient des lignes du style (sans guillemets):
    "PSHELL 12345678"
    "PSHELL 23425456"
    "PSHELL 54355454"

Je voudrais en fait prendre chaque ligne du fichier 2, et rajouter dans la ligne correspondante du fichier 1 des caractères supplémentaires. Pour prendre mon exemple ci-dessus:

J’ai dans le fichier 1 les lignes suivantes (toujours sans guillemets):
" ELSET=PSHELL 12345678,"
" ELSET=PSHELL 12345678_1,"
" ELSET=PSHELL 12345678_2,"

La ligne "PSHELL 12345678" est contenue dans le fichier 2

Je voudrais que dans le fichier 1 ce soit transformé en:
" ELSET=PSHELL 12345678,OFFSET=-0.5"
" ELSET=PSHELL 12345678_1,OFFSET=-0.5"
" ELSET=PSHELL 12345678_2,OFFSET=-0.5"
car la ligne "PSHELL 12345678" est contenue dans le fichier 2

Par contre je ne veux pas que soit modifiée la ligne " ELSET=PSHELL 88884855," (dans le fichier 1) car elle n’est pas contenue dans le fichier 2.

Vous auriez une idée de comment faire ceci avec un script bash, awk, tcsh peu importe?

Merci d’avance
ET

Salut,

Le fichier 1 :

cat fich_1
ELSET=PSHELL 12345678,
ELSET=PSHELL 12345678_1,
ELSET=PSHELL 12345678_2,
ELSET=PSHELL 88884855,
ELSET=PSHELL 23425456,
ELSET=PSHELL 54355454,

Le fichier 2 :

cat fich_2
PSHELL 12345678
PSHELL 23425456
PSHELL 54355454

Le script :

cat script.sh
#! /bin/bash

while read i
do
        grep `echo "$i" | cut -d " " -f 2` < fich_1 >> fich_tmp
done <fich_2

while read i
do
        sed -i.orig 's/'"$i"'/'"$i"'OFFSET=-0.5/' fich_1
done <fich_tmp

rm -f fich_tmp

Le résultat :


cat fich_1
ELSET=PSHELL 12345678,OFFSET=-0.5
ELSET=PSHELL 12345678_1,OFFSET=-0.5
ELSET=PSHELL 12345678_2,OFFSET=-0.5
ELSET=PSHELL 88884855,
ELSET=PSHELL 23425456,OFFSET=-0.5
ELSET=PSHELL 54355454,OFFSET=-0.5

:wink:

Bien joué :smiley:

Perso je l’ai fait en perl (pour m’entraîner, vu que j’apprends le perl) :

#!/usr/bin/perl

use strict;

open(fic1, "$ARGV[0]");
open(fic2, "$ARGV[1]");

my @tab1 = <fic1>;
my @tab2 = <fic2>;

open(fic3, ">$ARGV[0]");

foreach my $str1 (@tab1) {
        my $i=0;
        my $found=0;

        while($found==0 && $i<@tab2) {
                $tab2[$i] =~ /(.*)$/;
                if($str1 =~ /^ELSET=$1/ ) {
                        $found=1;
                }
                else {
                        $i++;
                }
        }

        if($found) {
                $str1 =~ /(.*)$/;
                print fic3 $1."OFFSET=-0.5\n";
        }
        else {
                print fic3 $str1;
        }
}

Marche tout aussi bien :smiley:

Lancer ./script fic1 fic2

Bonsoir
Merci pour votre aide. J’avais trouvé une solution après m’être fait pleins de noeuds au cerveau. J’ai utilisé awk et scilab, un truc de ouf. Je vois que vous savez faire plus simple :slight_smile:
Merci encore

ET

Bonsoir,

Voilà encore une vesion en Perl (dans l’ésprit TMTOWTDI)


#! /usr/bin/perl

use warnings;
use strict;
$"="";

open D,"./fic_jp0" or die "E/S : $!\n";
open F,"./fic_jp1" or die "E/S : $!\n";
open R,">./res.txt" or die "E/S : $!\n";

my @f = <F>;
my @d = <D>;

map { for my $e(@f){ if ( $_ =~ substr($e,7,8)){$_ =~ s/(?=$)/OFFSET=-0.5/} } }@d;
print R "@d";

Et le résultat


[lamitest@localhost corbeille]$ cat fic_jp0
ELSET=PSHELL 12345678,
ELSET=PSHELL 12345678_1,
ELSET=PSHELL 12345678_2,
ELSET=PSHELL 88884855,
ELSET=PSHELL 23425456,
ELSET=PSHELL 54355454,
[lamitest@localhost corbeille]$ cat fic_jp1
PSHELL 12345678
PSHELL 23425456
PSHELL 54355454
[lamitest@localhost corbeille]$ perl jp.pl
[lamitest@localhost corbeille]$ cat res.txt
ELSET=PSHELL 12345678,OFFSET=-0.5
ELSET=PSHELL 12345678_1,OFFSET=-0.5
ELSET=PSHELL 12345678_2,OFFSET=-0.5
ELSET=PSHELL 88884855,
ELSET=PSHELL 23425456,OFFSET=-0.5
ELSET=PSHELL 54355454,OFFSET=-0.5
[lamitest@localhost corbeille]$

Bonne soirée,

lami20j