# \[SED\] Remplacement d'une chaine par variable dans fichier

**URL:** <https://www.debian-fr.org/t/sed-remplacement-dune-chaine-par-variable-dans-fichier/66245>\
**Category:** Programmation\
**Created:** [Février 21, 2016, 9:44 UTC](https://www.debian-fr.org/t/sed-remplacement-dune-chaine-par-variable-dans-fichier/66245 "2016-02-21T21:44:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PengouinPdt](https://www.debian-fr.org/user_avatar/www.debian-fr.org/pengouinpdt/32/3978_2.png) [@PengouinPdt](https://www.debian-fr.org/u/PengouinPdt)\
**Post date:** [Février 21, 2016, 9:44 UTC](https://www.debian-fr.org/t/sed-remplacement-dune-chaine-par-variable-dans-fichier/66245/1 "2016-02-21T21:44:40Z")

</div>

Bonsoir,  
Ce soir, c’est SED qui m’énerve 😛

Voici le code BASH incriminé :

```auto
html="<article>
                <header><strong>${absolufichier}</strong> envoyé sur <em>${servicechoisi}</em><br/><time datetime=\"$(date +%Y-%m-%d\ %H:%M:%S)\">$(date +%d-%m-%Y\ à\ %H:%M:%S)</time></header>
                <img src=\""${minilog}"\" alt=\""${minilog}"\" /><br />
                <p>${mssg_url}<a href=\""${urlimage}"\">${urlimage}</a><br />
                ${mssg_mini}${minipourforum}</p>
                <footer><hr /></footer>
                </article>
                </body>"

                sed -i -e "s#</body>#$html#g" "${logname}"
```

Le contenu dynamique de la variable html aura ce style de valeur :

```auto
<article>
                <header><strong>/home/zou/Images/Capture d'écran - 21012015 - 17:18:49.png</strong> envoyé sur <em>Casimages.com</em><br/><time datetime="2015-06-24 18:42:02">24-06-2015 à 18:42:02</time></header>
                <img src="http://nsa38.casimages.com/img/2015/06/24/mini_150624064331250126.png" alt="http://nsa38.casimages.com/img/2015/06/24/mini_150624064331250126.png" /><br />
                <p>Accès à l'image : <a href="http://nsa38.casimages.com/img/2015/06/24/150624064331250126.png">http://nsa38.casimages.com/img/2015/06/24/150624064331250126.png</a><br />
                Miniature pour forum : [url=http://nsa38.casimages.com/img/2015/06/24/150624064331250126.png][img]http://nsa38.casimages.com/img/2015/06/24/mini_150624064331250126.png[/img][/url]</p>
                <footer><hr /></footer>
                </article>
                </body>

```

Le fichier “logname” est un simple fichier html, renfermant de base le code suivant :

```auto
<!doctype html>
<html lang="fr">
<head>
            <title>images sur les forums</title>
            <meta charset="utf-8" />
            <style type="text/css">
                a img { border:0; }
            </style>
        </head>
        <body>
        </body>
        </html>

```

Quand j’exécute le script Bash, il me répond :  
sed: -e expression n°1, caractère 19: commande `s’ inachevée

Je n’arrive pas à comprendre pourquoi !?

---

<div class="post-metadata">

**Author:** ![flavien](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/f/f475e1/32.png) [@flavien](https://www.debian-fr.org/u/flavien)\
**Post date:** [Février 21, 2016, 9:44 UTC](https://www.debian-fr.org/t/sed-remplacement-dune-chaine-par-variable-dans-fichier/66245/2 "2016-02-21T21:44:41Z")

</div>

Ta chaîne de remplacement contient des caractères de contrôle d’expression régulière (comme [et])  
Une piste pour les escaper : [http://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern](http://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern)

---

<div class="post-metadata">

**Author:** ![PengouinPdt](https://www.debian-fr.org/user_avatar/www.debian-fr.org/pengouinpdt/32/3978_2.png) [@PengouinPdt](https://www.debian-fr.org/u/PengouinPdt)\
**Post date:** [Février 21, 2016, 9:44 UTC](https://www.debian-fr.org/t/sed-remplacement-dune-chaine-par-variable-dans-fichier/66245/3 "2016-02-21T21:44:41Z")

</div>

J’ai trouvé !

Merci à toi, flavien, mais ce n’était pas cela.

À partir du moment où mon flux html se tient d’un seul tenant, éliminant les retours chariots … je n’ai plus de problème.

Pour info, mon code devient :

```auto
html="<article>"
html="${html}<header><strong>${absolufichier}</strong> envoyé sur <em>${servicechoisi}</em><br/><time datetime=\"$(date +%Y-%m-%d\ %H:%M:%S)\">$(date +%d-%m-%Y\ à\ %H:%M:%S)</time></header>"
html="${html}<p>$(cat "${tampon}" | tr -s ' ' | tr -d '\r\n' | sed "s#--#<br />#g")</p>"
html="${html}<footer><hr /></footer>"
html="${html}</article>"
html="${html}\n</body>"

if [[-n "${html}"]]; then
    sed -i -e "s#</body>#${html}#g" "${logname}"
fi

```
