Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change output of only the first line in bash script

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    #!/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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading