1

i did a script that retrieve content of a source file so i can create a target file the way i want it.

I do a "sed" command so i can change the word "Numérique" with "PIC S".

Source File .txt :

MotifRad;CHAR(2);Motif de radiation
MtPrime;Numérique 8.2;Montant prime d'origine

Target File .txt :

* Motif de radiation
05 MotifRad PIC X(2).
* Montant prime d'origine
05 MtPrime PIC S 8.2.

As you can see i did change the word "Numérique" but i'd like to add the number that follows in parentheses like that : PIC S (8.2), how could i do that ?

Bash script :

#!/bin/bash

#Fichier Source
fichier="APGFPOLI.des.txt"

champAdd="05 "

if [[ -f "$fichier" ]]
then
    
    # read it
    sed 1d $fichier| sed -i 's/CHAR/PIC X/' $fichier | sed -i 's/Numérique/PIC S/' $fichier | while IFS=';' read -r nomChamp format libelle
    do
        echo \* $libelle
        echo $champAdd $nomChamp $format.
    done <"$fichier" > test.txt
fi
4
  • Thanks for sharing your efforts. Your question is not 100% clear. Where do these 05 come from? And this PIC X(2)? Please explain exactly what you are trying to achieve, do not expect us to guess from your attempts. Commented Dec 15, 2021 at 13:50
  • The 05 come from my variable champAdd, i need to reformat my source so my target can be like that, same thing for the PIC X (2), as you can see in my source i had "CHAR (2)" but for my personnal reasons i need it to be PIC X. So my sed commands are here for that. What I need now is to add parantheses after my PIC S change. Instead of having PIC S 8.2 in my target i'd like to have PIC S (8.2) Commented Dec 15, 2021 at 13:53
  • 1
    OK, so your question is only about this Numérique line? I suggest to edit it and keep only this part. Commented Dec 15, 2021 at 13:55
  • Yes only for the "Numérique" line Commented Dec 15, 2021 at 13:56

2 Answers 2

1
$ sed 's/;Numérique[[:space:]]\+\([^;]*\)/;PIC S(\1)/' <<< "MtPrime;Numérique 8.2;Montant prime d'origine"
MtPrime;PIC S(8.2);Montant prime d'origine

Or, with GNU sed:

$ sed -E 's/;Numérique\s+([^;]*)/;PIC S(\1)/' <<< "MtPrime;Numérique 8.2;Montant prime d'origine"
MtPrime;PIC S(8.2);Montant prime d'origine

Applied to your file:

$ sed -E '/Numérique/s/;Numérique\s+([^;]*)/;PIC S(\1)/' file.txt
MotifRad;CHAR(2);Motif de radiation
MtPrime;PIC S(8.2);Montant prime d'origine

If you want to replace CHAR and Numérique in the same run:

$ sed -E '/CHAR/s/;CHAR(\([[:digit:]]+\));/;PIC X\1/
          /Numérique/s/;Numérique\s+([^;]*)/;PIC S(\1)/' file.txt
MotifRad;PIC X(2);Motif de radiation
MtPrime;PIC S(8.2);Montant prime d'origine
Sign up to request clarification or add additional context in comments.

3 Comments

I replace my sed "sed -i 's/Numérique/PIC S/' $fichier" by yours but it still give my the same output.
All I show has been tested on your exact example with GNU sed 4.7. If you do not see any difference with your own sed commands there must be something wrong with what you do. Do you modify in place (sed -i)? If yes your source file has already been modified by your own attempts. Restore it.
Okay i had a problem with my compilation, working perfectly. Thank you very much
1

I am only answering the portion regarding the replacement of Numérique [a floating point number] with PIC S [that same floating point number].

Use a capturing group to capture the floating point and a back reference to add it back to the replacement:

cat file
MotifRad;CHAR(2);Motif de radiation
MtPrime;Numérique 8.2;Montant prime d'origine

sed -E 's/Numérique([[:blank:]]+[[:digit:].]+)/PIC S\1/' file
MotifRad;CHAR(2);Motif de radiation
MtPrime;PIC S 8.2;Montant prime d'origine

If you want parenthesis around the replacement:

sed -E 's/Numérique([[:blank:]]+)([[:digit:].]+)/PIC S\1(\2)/' file
MotifRad;CHAR(2);Motif de radiation
MtPrime;PIC S (8.2);Montant prime d'origine

Capturing groups are defined with ([thing to capture]) and the reference to that captured item is \1, \2, \n with the left most opening parenthesis being the lowest number and so on.

Example:

echo 'this;that' | sed -E 's/(.*);(.*)/\2 \1/'
that this

3 Comments

I did this, but my output stay 8.2 and not (8.2)
sed -E 's/Numérique([[:blank:]]+)([[:digit:].]+)/PIC S\1(\2)/' file then
still nothing on my output

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.