Scripts bash

azari12 -  
zipe31 Messages postés 34620 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour a tous,

j'aurai besoin d'un petit conseil

j'ai un fichier file.conf
ainsi que 3 autres fichiers nommés a.txt b.txt c.txt
dans un premier temps je souhaite repeter n fois une chaine de caracteres (chaine-A, chaine-B, chaine-C) a la fin du fichier, pour ca pas trop complique une
boucle for , echo >> et ca devrait etre bon.
maintenant pour chaque chaines je voudrais (je vais faire un dessin !)

$cat a.txt
X.a.X.a.X.a.X

$cat b.txt
X.b.X.b.X.b.X

---------------------
$cat file.conf
chaine-A X.a.X.a.X.a.X
chaine A X.a.X.a.X.a.X
n-fois chaine-A
chaine-B X.b.X.b.X.b.X
n fois chaine-B
chaine-C X.a.X.a.X.a.X
n fois chaine-C

bash awk? sed? ou perl , j'attend vos conseils merci :)

1 réponse

  1. zipe31 Messages postés 34620 Date d'inscription   Statut Contributeur Dernière intervention   6 501
     
    Salut,

    Une solution avec "sed" :

    $ cat file.conf                                         
    chaine-AX.a.X.a.X.a.X                                 
    chaineAX.a.X.a.X.a.X                                  
    chaine-BX.b.X.b.X.b.X                                 
    chaine-CX.a.X.a.X.a.X                                  
    
    $ cat a.txt                                           
    X.a.X.a.X.a.X
    
    $ cat b.txt
    X.b.X.b.X.b.X
    
    $ A=$(cat a.txt)
    
    $ B=$(cat b.txt)
    
    $ echo $A
    X.a.X.a.X.a.X
    
    $ echo $B
    X.b.X.b.X.b.X
    
    $ sed -i.bakA '/^chaine-B/i\
    '"$A"'
    ' file.conf
    
    $ sed -i.bakB '/^chaine-C/i\
    '"$B"'
    ' file.conf
    
    $ cat file.conf
    chaine-A X.a.X.a.X.a.X
    chaine A X.a.X.a.X.a.X
    X.a.X.a.X.a.X
    chaine-B X.b.X.b.X.b.X
    X.b.X.b.X.b.X
    chaine-C X.a.X.a.X.a.X
    
    $

    ;-))
    0