• =?utf-8?Q?R=C3=A9cup=C3=A9rer_le_r=C3=A9sultat_d'une_commande_sed_da?=

    From [email protected]@21:1/5 to All on Thu Nov 4 03:00:02 2021
    Bonjour,

    Dans un script (bash 5.0.3(1), debian 10), je tente de récupérer le résultat d'une commande sed dans une variable.

    DOMAIN=http://toto.com/
    sed '/^abc\|^$/! { p; s|^| -> '$DOMAIN'|g }' mon_fichier

    Dans la commande, le séparateur de la substitution est | car avec / il y a un conflit avec le / de la cariable DOMAIN

    J'ai tenté diverses expressions, comme par exemple dans une variable :
    RES=$( sed '/^abc\|^$/! { p; s|^| -> '$DOMAIN'|g }')

    ou en écrivant dans un fichier, avec une subsitution de commande ou sans : $(sed '/^abc\|^$/! { p; s|^| -> '"$DOMAIN"'|g }' "$RES" > mon_fichier)

    ou sans :
    $(sed '/^abc\|^$/! { p; s|^| -> '"$DOMAIN"'|g }' "$RES" > mon_fichier)


    Dans le script, je n'arrive pas du tout à obtenir le résultat escompté, tant via la variable que par le fichier : RIEN, même pas le résultat aplati/sans séparateur que j'obtiens en ligne de commande ci-dessous.


    En ligne de commande, j'obtiens exactement le résultat escompté sur stdout ou dans le fichier.
    foo
    bar
    baz
    truc
    machin

    TOUTEFOIS, il y a un indice pour la question posée. Quand je fais :
    echo $RES

    J'obtiens le résultat aplati, sans séparateur :
    foo bar baz truc machin


    J'ai écumé de nombreux forums où ce problème est soulevé. Sans trouver la clef.

    J'ai tenté de prende une valeur de DOMAIN sans / pour une expression de sed avec des / au lieu de | : idem.

    Quel mystère y a-t-il qui m'échapperait donc ?
    Je me dis qu'il y a une option de sed qui m'échappe ou une obscure histoire de contexte bash.


    Merci pour vos lumières/bougies/loupiotes !

    Cordialement
    Roger

    <html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div>Bonjour,</div><div><br data-mce-bogus="1"></div><div>Dans un script (bash 5.0.3(1), debian 10), je tente de récupérer le résultat d'une commande
    sed dans une variable.</div><div><br data-mce-bogus="1"></div><div><span style="font-family: &quot;courier new&quot;, courier, monaco, monospace, sans-serif;" data-mce-style="font-family: 'courier new', courier, monaco, monospace, sans-serif;">&nbsp;
    DOMAIN=http://toto.com/</span></div><div><span style="font-family: &quot;courier new&quot;, courier, monaco, monospace, sans-serif;" data-mce-style="font-family: 'courier new', courier, monaco, monospace, sans-serif;">&nbsp; sed '/^abc\|^$/! { p; s|^|&
    nbsp; -&gt; '$DOMAIN'|g }'&nbsp; mon_fichier</span><br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><span style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures:
    normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #
    ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;" data-mce-style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 16px; font-style:
    normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-
    width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">Dans la commande, le&nbsp;</span>séparateur de la substitution est | car
    avec / il y a un conflit avec le / de la cariable DOMAIN&nbsp;</div><div><br data-mce-bogus="1"></div><div><div>J'ai tenté diverses expressions, comme par exemple dans une variable :</div><div><span style="font-family: &quot;courier new&quot;, courier,
    monaco, monospace, sans-serif;" data-mce-style="font-family: 'courier new', courier, monaco, monospace, sans-serif;">&nbsp; RES=$(<span style="color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps:
    normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-
    decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;" data-mce-style="color: #000000; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-
    caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-
    thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;">sed '/^abc\|^$/! { p; s|^|&nbsp; -&gt; '$DOMAIN'|g }')</span></span><div style="clear: both;" data-mce-style="clear: both;"><br><
    /div><div style="clear: both;" data-mce-style="clear: both;">ou en écrivant dans un fichier, avec une subsitution de commande ou sans :<br></div></div><div><span style="orphans: 2; text-align: start; text-indent: 0px; widows: 2; background-color: rgb(
    255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important; font-family: &quot;courier new&quot;, courier, monaco, monospace, sans-serif;" data-mce-style="
    orphans: 2; text-align: start; text-indent: 0px; widows: 2; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important; font-family: 'courier new',
    courier, monaco, monospace, sans-serif;">&nbsp; $(sed '/^abc\|^$/! { p; s|^|&nbsp; -&gt; '"$DOMAIN"'|g }' "$RES" &gt; mon_fichier)</span></div><div><span style="orphans: 2; text-align: start; text-indent: 0px; widows: 2; background-color: rgb(255, 255,
    255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;" data-mce-style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 16px; font-style:
    normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-
    width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"><br data-mce-bogus="1"></span></div><div><span style="orphans: 2; text-
    align: start; text-indent: 0px; widows: 2; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;" data-mce-style="color: #000000;
    font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none;
    white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"></
    span><span style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-
    indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display:
    inline !important; float: none;" data-mce-style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans:
    2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-
    decoration-color: initial; display: inline !important; float: none;">ou sans :</span><br></div><div><div><div style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-
    variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-
    decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" data-mce-style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-
    caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-
    thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><span data-mce-style="orphans: 2; text-align: start; text-indent: 0px; widows: 2; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style:
    initial; text-decoration-color: initial; float: none; display: inline !important; font-family: 'courier new', courier, monaco, monospace, sans-serif;" style="orphans: 2; text-align: start; text-indent: 0px; widows: 2; background-color: rgb(255, 255, 255);
    text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; font-family: &quot;courier new&quot;, courier, monaco, monospace, sans-serif; display: inline !important;">&nbsp; $(sed '/^abc\|^$/! { p; s|^
    |&nbsp; -&gt; '"$DOMAIN"'|g }' "$RES" &gt; mon_fichier)</span></div><div style="clear: both;" data-mce-style="clear: both;"><br data-mce-bogus="1"></div><div style="clear: both;" data-mce-style="clear: both;"><br data-mce-bogus="1"></div>Dans le script,
    je n'arrive pas du tout à obtenir le résultat escompté, tant via la variable que par le fichier : RIEN, même pas le résultat aplati/sans séparateur&nbsp; que j'obtiens en ligne de commande ci-dessous.</div><div><br></div><div><br></div><div style="
    clear: both;" data-mce-style="clear: both;"><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;">En ligne de commande, j'obtiens exactement le résultat escompté sur stdout ou dans le fichier.</span><br></div><div style="clear: both;" data-
    mce-style="clear: both;"><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;"><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;"><span data-mce-style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 16px;
    font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-
    text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;" style="orphans: 2; text-align: start; text-indent: 0px;
    widows: 2; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;"><span data-mce-style="color: #000000; font-family: 'courier new'
    , courier, monaco, monospace, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none;
    white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"
    style="color: rgb(0, 0, 0); font-family: &quot;courier new&quot;, courier, monaco, monospace, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2;
    text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-
    decoration-color: initial; float: none; display: inline !important;">&nbsp; foo</span></span></span></span></div><div style="clear: both;" data-mce-style="clear: both;"><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;"><span style="font-
    size: 12pt;" data-mce-style="font-size: 12pt;"><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;"><span data-mce-style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures:
    normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #
    ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;" style="orphans: 2; text-align: start; text-indent: 0px; widows: 2; background-color: rgb(255, 255, 255);
    text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;"><span data-mce-style="color: #000000; font-family: 'courier new', courier, monaco, monospace, sans-serif; font-
    size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;
    -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;" style="color: rgb(0, 0, 0); font-family: &quot;
    courier new&quot;, courier, monaco, monospace, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-
    transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none;
    display: inline !important;">&nbsp;<span>&nbsp;</span></span></span></span></span></span><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;"><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;"><span data-mce-style="color: #
    000000; font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform:
    none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float:
    none;" style="orphans: 2; text-align: start; text-indent: 0px; widows: 2; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;"><
    span data-mce-style="color: #000000; font-family: 'courier new', courier, monaco, monospace, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2;
    text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-
    color: initial; display: inline !important; float: none;" style="color: rgb(0, 0, 0); font-family: &quot;courier new&quot;, courier, monaco, monospace, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps:
    normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-
    decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;">bar</span></span></span></span></div><div style="clear: both;" data-mce-style="clear: both;"><span style="font-size:
    12pt;" data-mce-style="font-size: 12pt;"><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;"><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;"><span data-mce-style="color: #000000; font-family: arial, helvetica, sans-serif;
    font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing:
    0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;" style="orphans: 2; text-align: start; text-
    indent: 0px; widows: 2; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;"><span data-mce-style="color: #000000; font-family:
    'courier new', courier, monaco, monospace, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-
    transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important;
    float: none;" style="color: rgb(0, 0, 0); font-family: &quot;courier new&quot;, courier, monaco, monospace, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing:
    normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-
    style: initial; text-decoration-color: initial; float: none; display: inline !important;">&nbsp;<span>&nbsp;</span></span></span></span></span></span><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;"><span style="font-size: 12pt;" data-mce-
    style="font-size: 12pt;"><span data-mce-style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2;
    text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-
    color: initial; display: inline !important; float: none;" style="orphans: 2; text-align: start; text-indent: 0px; widows: 2; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color:
    initial; float: none; display: inline !important;"><span data-mce-style="color: #000000; font-family: 'courier new', courier, monaco, monospace, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal;
    font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness:
    initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;" style="color: rgb(0, 0, 0); font-family: &quot;courier new&quot;, courier, monaco, monospace, sans-serif; font-size: 16px; font-style:
    normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-
    width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;">baz</span></span></span></span></div><div style="clear: both;"
    data-mce-style="clear: both;"><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;"><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;"><span data-mce-style="color: #000000; font-family: arial, helvetica, sans-serif; font-size:
    16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -
    webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;" style="orphans: 2; text-align: start; text-indent: 0px;
    widows: 2; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;"><span data-mce-style="color: #000000; font-family: 'courier
    new', courier, monaco, monospace, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none;
    white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"
    style="color: rgb(0, 0, 0); font-family: &quot;courier new&quot;, courier, monaco, monospace, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2;
    text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-
    decoration-color: initial; float: none; display: inline !important;">&nbsp; truc</span></span></span></span></div><div style="clear: both;" data-mce-style="clear: both;"><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;"><span style="font-
    size: 12pt;" data-mce-style="font-size: 12pt;"><span data-mce-style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-
    spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-thickness: initial; text-decoration-
    style: initial; text-decoration-color: initial; display: inline !important; float: none;" style="orphans: 2; text-align: start; text-indent: 0px; widows: 2; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style:
    initial; text-decoration-color: initial; float: none; display: inline !important;"><span data-mce-style="color: #000000; font-family: 'courier new', courier, monaco, monospace, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures:
    normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #
    ffffff; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;" style="color: rgb(0, 0, 0); font-family: &quot;courier new&quot;, courier, monaco, monospace, sans-serif;
    font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing:
    0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;">&nbsp; machin</span></span></span></
    span><div data-mce-style="clear: both;" style="clear: both;"><br></div></div></div></div><div><div><div>TOUTEFOIS, il y a un indice pour la question posée. Quand je fais :</div><div><span style="font-family: &quot;courier new&quot;, courier, monaco,
    monospace, sans-serif;" data-mce-style="font-family: 'courier new', courier, monaco, monospace, sans-serif;">&nbsp; echo $RES</span></div><div><span style="font-family: arial, helvetica, sans-serif;" data-mce-style="font-family: arial, helvetica, sans-
    serif;"><br data-mce-bogus="1"></span></div><div><span style="font-family: arial, helvetica, sans-serif;" data-mce-style="font-family: arial, helvetica, sans-serif;">J'obtiens le résultat aplati, sans séparateur :</span></div><span style="font-family: &
    quot;courier new&quot;, courier, monaco, monospace, sans-serif;" data-mce-style="font-family: 'courier new', courier, monaco, monospace, sans-serif;">&nbsp; foo bar baz truc machin</span></div><div><br data-mce-bogus="1"></div></div><div><br></div><div>J'
    ai écumé de nombreux forums où ce problème est soulevé. Sans trouver la clef.</div><div><br data-mce-bogus="1"></div><div>J'ai tenté de prende une valeur de DOMAIN sans / pour une expression de sed avec des / au&nbsp; lieu de | : idem.&nbsp;</div><
    <br data-mce-bogus="1"></div><div>Quel mystère y a-t-il qui m'échapperait donc ?</div><div>Je me dis qu'il y a une option de sed qui m'échappe ou une obscure histoire de contexte bash.</div><div><br></div><div><br data-mce-bogus="1"></div><div><
    span style="font-size: 12pt;" data-mce-style="font-size: 12pt;">Merci pour vos lumières/bougies/loupiotes !</span></div><div><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;"><br data-mce-bogus="1"></span></div><div><span style="font-size:
    12pt;" data-mce-style="font-size: 12pt;">Cordialement</span></div><div><span style="font-size: 12pt;" data-mce-style="font-size: 12pt;">Roger</span></div></div></body></html>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Pierre Malard@21:1/5 to All on Thu Nov 4 08:20:01 2021
    --Apple-Mail=_15872AC0-823C-4C34-9E25-15A4519CEEB9
    Content-Transfer-Encoding: quoted-printable
    Content-Type: text/plain;
    charset=utf-8

    Bonjour,

    C’est normal avec l’affichage d’une variable contenant des LF. Ce serait la même chose avec un :
    A=$(ls)

    Une piste tient dans la façon que vous utilisez pour afficher le résultat de la variable. En effet, un
    echo ${A}
    comme vous le faite n’affichera pas la même chose qu’un :
    echo "${A}"

    Faite l’essai, enclore une variable avec des double-guillemets est souvent non seulement plus clair et ne provoque pas une interprétation supplémentaire par le BASH.

    En plus l’utilisation des accolades est aussi un moyen d’éviter les ambiguïtés :
    ${A}B
    n’est pas forcément la même chose que :
    ${AB}
    ou $AB



    Le 4 nov. 2021 à 02:49, [email protected] a écrit :

    Bonjour,

    Dans un script (bash 5.0.3(1), debian 10), je tente de récupérer le résultat d'une commande sed dans une variable.

    DOMAIN=http://toto.com/
    sed '/^abc\|^$/! { p; s|^| -> '$DOMAIN'|g }' mon_fichier

    Dans la commande, le séparateur de la substitution est | car avec / il y a un conflit avec le / de la cariable DOMAIN

    J'ai tenté diverses expressions, comme par exemple dans une variable :
    RES=$(sed '/^abc\|^$/! { p; s|^| -> '$DOMAIN'|g }')

    ou en écrivant dans un fichier, avec une subsitution de commande ou sans :
    $(sed '/^abc\|^$/! { p; s|^| -> '"$DOMAIN"'|g }' "$RES" > mon_fichier)

    ou sans :
    $(sed '/^abc\|^$/! { p; s|^| -> '"$DOMAIN"'|g }' "$RES" > mon_fichier)


    Dans le script, je n'arrive pas du tout à obtenir le résultat escompté, tant via la variable que par le fichier : RIEN, même pas le résultat aplati/sans séparateur que j'obtiens en ligne de commande ci-dessous.


    En ligne de commande, j'obtiens exactement le résultat escompté sur stdout ou dans le fichier.
    foo
    bar
    baz
    truc
    machin

    TOUTEFOIS, il y a un indice pour la question posée. Quand je fais :
    echo $RES

    J'obtiens le résultat aplati, sans séparateur :
    foo bar baz truc machin


    J'ai écumé de nombreux forums où ce problème est soulevé. Sans trouver la clef.

    J'ai tenté de prende une valeur de DOMAIN sans / pour une expression de sed avec des / au lieu de | : idem.

    Quel mystère y a-t-il qui m'échapperait donc ?
    Je me dis qu'il y a une option de sed qui m'échappe ou une obscure histoire de contexte bash.


    Merci pour vos lumières/bougies/loupiotes !

    Cordialement
    Roger

    --
    Pierre Malard

    « On ne peut pas pousser à fond l'éducation politique et l'éducation
    tout court de masses sans l'accompagner d'un développement
    économique, culturel et social parallèle. »
    Romain Gary - "Les racines du ciel"
    |\ _,,,---,,_
    /,`.-'`' -. ;-;;,_
    |,4- ) )-,_. ,\ ( `'-'
    '---''(_/--' `-'\_) πr

    perl -e '$_=q#: 3|\ 5_,3-3,2_: 3/,`.'"'"'`'"'"' 5-. ;-;;,_: |,A- ) )-,_. ,\ ( `'"'"'-'"'"': '"'"'-3'"'"'2(_/--'"'"' `-'"'"'\_): 24πr::#;y#:#\n#;s#(\D)(\d+)#$1x$2#ge;print'
    - --> Ce message n’engage que son auteur <--


    --Apple-Mail=_15872AC0-823C-4C34-9E25-15A4519CEEB9
    Content-Transfer-Encoding: quoted-printable
    Content-Type: text/html;
    charset=utf-8


    [continued in next message]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marc Chantreux@21:1/5 to All on Thu Nov 4 17:00:02 2021
    Bonjour,

    J'ai tent� diverses expressions, comme par exemple dans une variable :
    RES=$( sed '/^abc\|^$/! { p; s|^| -> '$DOMAIN'|g }')

    * pas test� mais je doute que ton script passe avec autre chose que GNU sed.
    * c'est tr�s g�n�ralement une mauvaise id�e de ne pas prot�ger le
    r�sultat d'une commande avec ""

    donc j'aurais tendance � �crire

    RES="$( sed "
    /./!d
    /^abc/d
    p
    s|^| -> $DOMAIN|g
    ")"

    mais �crit comme ca, sed lit stdin alors que dans

    $(sed '/^abc\|^$/! { p; s|^| -> '"$DOMAIN"'|g }' "$RES" > mon_fichier)

    ca lit dans un fichier qui s'appelle "$RES" et ca �crit dans mon_fichier

    En ligne de commande, j'obtiens exactement le r�sultat escompt� sur stdout ou dans le fichier.

    * tappe la commande
    * si tout s'affiche bien, tappe

    RES="$(!!)"; echo "$RES"

    normallement t'as la m�me chose. non ?

    J'obtiens le r�sultat aplati, sans s�parateur :
    foo bar baz truc machin

    c'est normal! il faut que tu comprennent la mani�re dont fonctionnne les variable expansions. dans ton cas tu dois prot�ger l'expansion 2*:

    * le moment ou il capture

    RES="$( sed "
    /./!d
    /^abc/d
    p
    s|^| -> $DOMAIN|g
    ")"

    et le moment ou tu affiches

    echo "$RES" # qui ne fait pas du tout la meme chose que echo $RES

    c'est parce que l'expansion de la variable se fait sur IFS:

    echo -n "$IFS"|xxd
    # les chaines des variables sont split�es avec " " "\t" et "\n"

    si tu n'es pas sur: comme je le disais plus haut: mets des "" partout :)

    Quel myst�re y a-t-il qui m'�chapperait donc ?

    si tu nous proposais un script avec une entr�e et une sortie du genre

    <<. cat > from

    abc def
    abc def
    abc def

    ok 1
    ok 2

    .
    <<. cat > expected
    perdu.comok 1
    perdu.comok 2
    .
    DOMAIN=perdu.com
    RES="$( sed "
    /./!d
    /^abc/d
    s|^| -> $DOMAIN|g
    " mon_sample )"
    echo "$RES" > got
    diff got expected

    on pourrait probablement trouver le probl�me plus rapidement.

    cordialement,

    --
    Marc Chantreux
    Direction du num�rique de l'Universit� de Strasbourg
    P�le de Calcul et Services Avanc�s � la Recherche (CESAR) http://annuaire.unistra.fr/p/20200

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marc Chantreux@21:1/5 to All on Thu Nov 4 19:10:01 2021
    Le Thu, Nov 04, 2021 at 05:18:00PM +0100, [email protected] a �crit :

    En pr�ambule:

    <<. cat > monfichier
    abc-----
    foo
    bar

    abc-----
    baz
    .

    Maintenant je peux mettre en �vidence l'importance des quotes dans l'interpolation:

    set $( grep -v abc monfichier )
    echo $# argument dont le premier est $1
    set "$( grep -v abc monfichier )"
    echo $# argument dont le premier est $1
    RES=$( grep -v abc monfichier )
    set $RES
    echo $# argument dont le premier est $1
    set "$RES"
    echo $# argument dont le premier est $1

    qui me donne:

    3 argument dont le premier est foo
    1 argument dont le premier est foo bar baz
    3 argument dont le premier est foo
    1 argument dont le premier est foo bar baz

    donc maintenant:

    DOMAIN=http://toto.com
    sed -rn "p ; /^abc/d ; s,.+, -> $DOMAIN/&,p" monfichier
    # CA marche

    DOMAIN=http://toto.com
    RES=$( sed -rn "p ; /^abc/d ; s,.+, -> $DOMAIN/&,p" monfichier )
    echo "$RES"
    # CA marche

    DOMAIN=http://toto.com
    RES=$( sed -rn "p ; /^abc/d ; s,.+, -> $DOMAIN/&,p" monfichier )
    echo $RES
    # ca marche pas

    mieux ? ou alors n'ai pas vraiment compris le probl�me ?

    --
    Marc Chantreux
    Direction du num�rique de l'Universit� de Strasbourg
    P�le de Calcul et Services Avanc�s � la Recherche (CESAR) http://annuaire.unistra.fr/p/20200

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marc Chantreux@21:1/5 to All on Thu Nov 4 22:10:02 2021
    $ TRUC=$(cat monfichier)
    $ RES=$(sed '/^abc\|^$/! { p; s|^| -> '"${DOMAIN}"'|g }' "$TRUC")



    sed: can't read
    abc-----
    foo
    bar

    abc-----
    baz
    : No such file or directory:


    Idem avec :
    $ sed '/^abc\|^$/! { p; s|^| -> '"${DOMAIN}"'|g }' "$TRUC"


    ***
    Je me dis donc qu'il faut donner � sed l'argument qu'il attend : cad un nom de fichier.
    Or, ce n'est pas le cas car je lui donne la valeur d'une variable.
    Et il me r�pond qu'il cherche � ouvrir un fichier qui n'existe pas "No such file or directory".

    Finalement, j'ai tent� d'utiliser un pipe ET CA MARCHE !!!
    RES=$(echo "$TRUC" | sed '/^abc\|^$/! { p; s|^| -> '"${DOMAIN}"'|g }')

    ben ... ok j'�tais loin de m'imaginer que tu voulais vraiment filer le
    contenu d'une variable comme argument de sed mais oui en reprenant tes
    mails: c'�tait bien not� :(

    sed (comme beaucoup de filtres) une liste de fichiers a traiter en
    argument (ou alors stdin par defaut)

    au passage du coup:

    TRUC=$( cat monfichier )
    RES=$(echo "$TRUC" | sed '/^abc\|^$/! { p; s|^| -> '"${DOMAIN}"'|g }')

    s'�crit en fait

    sed '/^abc\|^$/! { p; s|^| -> '"${DOMAIN}"'|g }' monfichier

    ou si tu veux utiliser les redirections pour avoir le nom du fichier
    devant (c'est plus simple � la relecture)

    < monfichier sed '/^abc\|^$/! { p; s|^| -> '"${DOMAIN}"'|g }'

    --
    Marc Chantreux
    Direction du num�rique de l'Universit� de Strasbourg
    P�le de Calcul et Services Avanc�s � la Recherche (CESAR) http://annuaire.unistra.fr/p/20200

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Charles Plessy@21:1/5 to All on Fri Nov 5 01:50:01 2021
    Le Fri, Nov 05, 2021 at 12:31:13AM +0100, [email protected] a �crit :

    Comment expliquer que la commande prend un fichier comme argument :
    sed '/^abc\|^$/! { p; s|^| -> '"${DOMAIN}"'|g }' monfichier #SUCCES

    mais refuse de prendre la valeur d'une variable ?
    sed '/^abc\|^$/! { p; s|^| -> '"${DOMAIN}"'|g }' "$TRUC" #ECHEC

    Bonjour Roger,

    c'est comme �a que sed (et beaucoup d'autres outils) a �t� con�u: op�rer
    sur le contenu d'un fichier dont le nom est pass� en argument, ou alors
    sur un flux passant par l'entr�e standard.

    echo 'toto' | sed 's/o/a/g' # renvoie tata
    echo 'toto' > monfichier
    sed 's/o/a/g' monfichier # renvoie tata
    TRUC=monfichier
    sed 's/o/a/g' "$TRUC" # renvoie tata

    La variable $TRUC est remplac�e par sa valeur avant que sed ne soit
    execut�. La commande execut�e avec monfichier ou $TRUC est donc
    exactement la m�me.

    Bonne journ�e

    --
    Charles Plessy Nagahama, Yomitan, Okinawa, Japan
    Debian Med packaging team http://www.debian.org/devel/debian-med Tooting from work, https://mastodon.technology/@charles_plessy Tooting from home, https://framapiaf.org/@charles_plessy

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marc Chantreux@21:1/5 to All on Fri Nov 5 07:40:02 2021
    salut,

    Le Fri, Nov 05, 2021 at 12:31:13AM +0100, [email protected] a �crit :
    Et je n'ai m�me pas de fichier mais une variable � traiter.

    de mani�re g�n�rale:
    * passe par des pipes ou des fichiers plutot que des variables
    * utilise tee et mkfifo quand le probleme d'aiguillage est complexe.

    J'avais cr�� un fichier pour tenter de d�bloquer la situation avec une situation plus connue avec sed.

    Ca n'est pas la plus connue: c'est la seule :) et presque tous les filtres fonctionnent de m�me

    sed 'des trucs a faire' fichier1 fichier2 ... fichierN
    awk 'des trucs a faire' fichier1 fichier2 ... fichierN
    grep 'un motif � trouver' fichier1 fichier2 ... fichierN

    stdin est le fichier par defaut:

    ls | sed 's/.*/* [&](&)/' | cmark

    TRUC est fournie par un traitement pr�c�dent du script.

    alors utilise un fichier plutot qu'une variable

    ./ton_precedent_script > TRUC
    < TRUC sed '...'

    si tu n'as pas besoin de truc, tu peux directement piper a sed

    ./ton_precedent_script | sed ...

    enfin si tu veux les 2 (enregistrer TRUC et passer a sed en meme temps),
    tu peux faire un tee qui est une maniere efficace de dupliquer un flot
    (ca fait appel � l'appel syst�me du meme nom)

    ./ton_precedent_script | tee TRUC | sed '...'

    --
    Marc Chantreux
    Direction du num�rique de l'Universit� de Strasbourg
    P�le de Calcul et Services Avanc�s � la Recherche (CESAR) http://annuaire.unistra.fr/p/20200

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)