Here is my source :
TABLE;APGFPOLI;
And here is the output that i want :
01 APGFPOLI.
So what i am trying to do is remove "TABLE" and add 01 before "APGFPOLI". I need to do it for the first line only. So i tried to do :
#!/bin/bash
#Fichier Source
fichier="APGFPOLI.des.txt"
champAdd="05 "
if [[ -f "$fichier" ]]
then
# read it
sed -i '1 s/TABLE//' $fichier |sed -i 's/CHAR/PIC X/' $fichier | sed -E '/Numérique/s/;Numérique\s ([^;]*)/;PIC 9(\1)/' $fichier | while IFS=';' read -r nomChamp format libelle
do
echo \* $libelle
echo $champAdd $nomChamp $format.
done > test.txt
fi
As you can see, my first sed is supposed to remove TABLE but it dont work. i also do a echo for my others line but i'd like to echo this specific first line too.
Here is the output my bash gives me :
*
05 TABLE APGFPOLI.
Here is my full source if it helps :
TABLE;APGFPOLI;
Contrat;CHAR(16);Numéro du contrat
Libelle;CHAR(30);Libellé du contrat
DtCreation;CHAR(8);Date de création
DtMaj;CHAR(8);Date de dernière MAJ
DtEffet;CHAR(8);Date d'effet adhésion
MotifAdh;CHAR(2);Motif d'adhésion
DtRadiation;CHAR(8);Date de radiation
DtEnrRad;CHAR(8);Date enregistrement radiat
MotifRad;CHAR(2);Motif de radiation
MtPrime;Numérique 8.2;Montant prime d'origine
DtEffetSusp;CHAR(8);Date d'effet de suspension
DtFinSusp;CHAR(8);Date de fin de suspension
MotifSusp;CHAR(2);Motif de suspension
DestBord;CHAR(1);Destinataire du bordereau
CdDest;CHAR(5);Code du destinataire
NivRupBord;CHAR(1);Niveau rupture bordereau
BordCETIP;CHAR(1);Bordereau CTIP
EnvBordNom;CHAR(1);Envoi bordereau nominatif
Indice;CHAR(2);Indice appliqué
Echeance;CHAR(2);Echéance de l'indice (MM)
Effectif;CHAR(5);Effectif
CdRegr;CHAR(3);Code regroupement 1
CdGroupe;CHAR(3);Code regroupement 2
Periodicite;CHAR(1);Périodicité
Terme;CHAR(1);Terme
Produit;CHAR(6);Code produit affecté
Inspecteur;CHAR(5);Inspecteur
CleInsp;CHAR(1);Clé inspecteur
Filler;CHAR(6);Filler
CodePudding user response:
You generally don't want to run sed -i
on the same file more than once.
Your entire task can be rephrased into just
sed -i '1s/TABLE/01 /' APGFPOLI.des.txt
If the replacement string should come from a shell variable, you need to use double quotes instead of single:
replacement="05"
sed -i "1s/TABLE/$replacement /" APGFPOLI.des.txt
If you want to keep your other tasks (which are not documented in the question) you can easily merge them into the same script. Remember, sed
is a scripting language; you can feed in arbitrarily complex scripts (and some crazy people have even implemented desk calculators and Game of Life in sed
scripts).
sed -i -E -e '1 s/TABLE/01 /' -e 's/CHAR/PIC X/' \
-e '/Numérique/s/;Numérique\s ([^;]*)/;PIC 9(\1)/' APGFPOLI.des.txt
If after this you want to pull out the result and display it with some decorations, I would add a second sed
script without -i
so that it displays the output on standard output without modifying the file.
sed '1s/\([^;]*\);\([^;]*\);\([^;]*\);.*/* \3\n05 \2 \1/;q' APGFPOLI.des.txt
CodePudding user response:
For removing TABLE;
from the first line and to prepend 01
, you just have to do:
fichier="APGFPOLI.des.txt"
sed -i '1s/^TABLE;/01 /' "$fichier"
When you ask something in SO, it is advisable to remove the part of the code that is not relevant to your problem, like the command sed -i 's/CHAR/PIC X/' $fichier
and so on.