[Shell] cut dans un sed

[Fermé]
Signaler
Messages postés
429
Date d'inscription
mardi 26 juin 2007
Statut
Membre
Dernière intervention
9 mai 2016
-
Messages postés
36324
Date d'inscription
dimanche 7 novembre 2010
Statut
Contributeur
Dernière intervention
27 janvier 2021
-
Bonjour,

J'ai un fichier texte, objets.txt, contenant plusieurs chaines de caractères délimitées par une virgule :

chaine1,chaine2,chaine3,chaine4,chaine5,chaine6,chaine7,chaine8,chaine9,chaine10,chaine11,chaine12,etc.

Je souhaiterais extraire, par exemple, les 3 premières chaines, et les injecter dans fichier1.txt dans un endroit précis, puis les 3 chaines suivantes, injecter dans fichier2.txt au même endroit précis, puis les 3 chaines suivantes, etc.

Les fichiers d'origines sont sous cette forme :

select distinct c.classnamea2a2, c.namecontainerinfo
from epmdocumentmaster m, ((select classnamea2a2, ida2a2, namecontainerinfo from project2)
union all (select classnamea2a2, ida2a2, namecontainerinfo from wtlibrary)
union all (select classnamea2a2, ida2a2, namecontainerinfo from pdmlinkproduct)) c
where m.name in (XXXXXXXXXX)
and m.CLASSNAMEKEYCONTAINERREFEREN=c.CLASSNAMEA2A2
and m.IDA3CONTAINERREFERENCE=c.IDA2A2;


Et j'aimerais remplacer XXXXXXXXXX par chaine1,chaine2,chaine3

Ce qui donnerait :

Fichier1.txt
select distinct c.classnamea2a2, c.namecontainerinfo
from epmdocumentmaster m, ((select classnamea2a2, ida2a2, namecontainerinfo from project2)
union all (select classnamea2a2, ida2a2, namecontainerinfo from wtlibrary)
union all (select classnamea2a2, ida2a2, namecontainerinfo from pdmlinkproduct)) c
where m.name in (chaine1,chaine2,chaine3)
and m.CLASSNAMEKEYCONTAINERREFEREN=c.CLASSNAMEA2A2
and m.IDA3CONTAINERREFERENCE=c.IDA2A2;


Voici mon script actuel, qui ne fonctionne pas :

#!/bin/bash
compte=1
d=-19
f=0
while [ $compte != 10 ]
do
var='d=$(($d+20)); f=$(($f+20)); cut -d"," -f$d-$f objets.txt' ###ici, les ' sont des cotes (altgr - 7), mais n'apparaissent pas correctement###
sed 's/XXXXXXXXXX/$var/' fichiers/fichier$compte.txt >> fichiers/fichier$compte.txt
compte=$(($compte+1))
done


Une petite aide ?

Merci.

1 réponse

Messages postés
36324
Date d'inscription
dimanche 7 novembre 2010
Statut
Contributeur
Dernière intervention
27 janvier 2021
6 437
Salut,

$ ls -1
fichier1.txt
fichier2.txt
fichier3.txt
fichier4.txt
foo.sh*
objets.txt

$ cat foo.sh 
#! /bin/bash

#set -xv

compte=1

while read line
do
sed -i.bak "s/XXXXXXXXXX/${line}/" fichier${compte}.txt
((compte++))
done < <(sed 's/\([^,]*,[^,]*,[^,]*\),/\1\n/g' objets.txt)

$ cat objets.txt 
chaine1,chaine2,chaine3,chaine4,chaine5,chaine6,chaine7,chaine8,chaine9,chaine10,chaine11,chaine12

$ cat fichier1.txt 
select distinct c.classnamea2a2, c.namecontainerinfo
from epmdocumentmaster m, ((select classnamea2a2, ida2a2, namecontainerinfo from project2)
union all (select classnamea2a2, ida2a2, namecontainerinfo from wtlibrary)
union all (select classnamea2a2, ida2a2, namecontainerinfo from pdmlinkproduct)) c
where m.name in (XXXXXXXXXX)
and m.CLASSNAMEKEYCONTAINERREFEREN=c.CLASSNAMEA2A2
and m.IDA3CONTAINERREFERENCE=c.IDA2A2;

$ cat fichier2.txt 
select distinct c.classnamea2a2, c.namecontainerinfo
from epmdocumentmaster m, ((select classnamea2a2, ida2a2, namecontainerinfo from project2)
union all (select classnamea2a2, ida2a2, namecontainerinfo from wtlibrary)
union all (select classnamea2a2, ida2a2, namecontainerinfo from pdmlinkproduct)) c
where m.name in (XXXXXXXXXX)
and m.CLASSNAMEKEYCONTAINERREFEREN=c.CLASSNAMEA2A2
and m.IDA3CONTAINERREFERENCE=c.IDA2A2;

$ cat fichier3.txt 
select distinct c.classnamea2a2, c.namecontainerinfo
from epmdocumentmaster m, ((select classnamea2a2, ida2a2, namecontainerinfo from project2)
union all (select classnamea2a2, ida2a2, namecontainerinfo from wtlibrary)
union all (select classnamea2a2, ida2a2, namecontainerinfo from pdmlinkproduct)) c
where m.name in (XXXXXXXXXX)
and m.CLASSNAMEKEYCONTAINERREFEREN=c.CLASSNAMEA2A2
and m.IDA3CONTAINERREFERENCE=c.IDA2A2;

$ cat fichier4.txt 
select distinct c.classnamea2a2, c.namecontainerinfo
from epmdocumentmaster m, ((select classnamea2a2, ida2a2, namecontainerinfo from project2)
union all (select classnamea2a2, ida2a2, namecontainerinfo from wtlibrary)
union all (select classnamea2a2, ida2a2, namecontainerinfo from pdmlinkproduct)) c
where m.name in (XXXXXXXXXX)
and m.CLASSNAMEKEYCONTAINERREFEREN=c.CLASSNAMEA2A2
and m.IDA3CONTAINERREFERENCE=c.IDA2A2;

$ ./foo.sh 

$ ls -1
fichier1.txt
fichier1.txt.bak
fichier2.txt
fichier2.txt.bak
fichier3.txt
fichier3.txt.bak
fichier4.txt
fichier4.txt.bak
foo.sh*
objets.txt

$ cat fichier1.txt 
select distinct c.classnamea2a2, c.namecontainerinfo
from epmdocumentmaster m, ((select classnamea2a2, ida2a2, namecontainerinfo from project2)
union all (select classnamea2a2, ida2a2, namecontainerinfo from wtlibrary)
union all (select classnamea2a2, ida2a2, namecontainerinfo from pdmlinkproduct)) c
where m.name in (chaine1,chaine2,chaine3)
and m.CLASSNAMEKEYCONTAINERREFEREN=c.CLASSNAMEA2A2
and m.IDA3CONTAINERREFERENCE=c.IDA2A2;

$ cat fichier2.txt 
select distinct c.classnamea2a2, c.namecontainerinfo
from epmdocumentmaster m, ((select classnamea2a2, ida2a2, namecontainerinfo from project2)
union all (select classnamea2a2, ida2a2, namecontainerinfo from wtlibrary)
union all (select classnamea2a2, ida2a2, namecontainerinfo from pdmlinkproduct)) c
where m.name in (chaine4,chaine5,chaine6)
and m.CLASSNAMEKEYCONTAINERREFEREN=c.CLASSNAMEA2A2
and m.IDA3CONTAINERREFERENCE=c.IDA2A2;

$ cat fichier3.txt 
select distinct c.classnamea2a2, c.namecontainerinfo
from epmdocumentmaster m, ((select classnamea2a2, ida2a2, namecontainerinfo from project2)
union all (select classnamea2a2, ida2a2, namecontainerinfo from wtlibrary)
union all (select classnamea2a2, ida2a2, namecontainerinfo from pdmlinkproduct)) c
where m.name in (chaine7,chaine8,chaine9)
and m.CLASSNAMEKEYCONTAINERREFEREN=c.CLASSNAMEA2A2
and m.IDA3CONTAINERREFERENCE=c.IDA2A2;

$ cat fichier4.txt 
select distinct c.classnamea2a2, c.namecontainerinfo
from epmdocumentmaster m, ((select classnamea2a2, ida2a2, namecontainerinfo from project2)
union all (select classnamea2a2, ida2a2, namecontainerinfo from wtlibrary)
union all (select classnamea2a2, ida2a2, namecontainerinfo from pdmlinkproduct)) c
where m.name in (chaine10,chaine11,chaine12)
and m.CLASSNAMEKEYCONTAINERREFEREN=c.CLASSNAMEA2A2
and m.IDA3CONTAINERREFERENCE=c.IDA2A2;

$

;-))
1
Merci

Quelques mots de remerciements seront grandement appréciés. Ajouter un commentaire

CCM 41989 internautes nous ont dit merci ce mois-ci