Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

R̓©cup̓©rer le r̓©sultat d'une commande sed dans une variable ou un fichier : probl̓¨me...

9 réponses
Avatar
roger.tarani
--=_e9bdb210-efc0-433f-b0f2-8703e57c0e44
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

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

--=_e9bdb210-efc0-433f-b0f2-8703e57c0e44
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<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><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>
--=_e9bdb210-efc0-433f-b0f2-8703e57c0e44--

9 réponses

Avatar
Marc Chantreux
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
Avatar
roger.tarani
Bonjour,
Je réagis rapidement :
Ma commande sed fait le boulot attendu en CLI si je prends un nom de fichier en argument, lequel fichier contient la chaͮne multilignes ͠ traiter.
Elle ne le fait pas quand je veux lire la chaÍ®ne multilignes qui est la MEME chaÍ®ne multilignes, qui est la valeur d'une variable précédemment définie (ici, TRUC).
Un nom de fichier est une chose : la commande va chercher ce que contient ce fichier.
Une variable en est une autre : la commande va lire la valeur de la variable.
... ou pas... J'ai un blanc... il doit manquer qqchose...
Faut-il effectuer une opération sur la variable TRUC qui contient la même chose que le fichier mon_fichier, pour pouvoir donner Í  sed l'argument qu'il attend (cad un nom de fichier) ?...
Rappel :
Le fichier contient une liste de lignes (dont certaines sont vides ou commencent par abc, sur lesquelles on ne fait aucun traitement ; mais c'est accessoire)
abc-----
foo
bar
abc-----
baz
Après le traitement, on veut :
abc-----
foo
-> http://toto.com/foo
bar
-> http://toto.com/bar
abc-----
baz
-> http://toto.com/baz
CECI FONCTIONNE
$ sed '/^abc|^$/! { p; s|^| -> '"${DOMAIN}"'|g }' mon_fichier
(affiche le résultat visé)
$ RES=$(sed '/^abc|^$/! { p; s|^| -> '"${DOMAIN}"'|g }' mon_fichier)
$ echo "$RES"
(affiche le résultat visé)
CECI NE FONCTIONNE PAS
A présent, mettons la chaÍ®ne multi-lignes Í  traiter dans une variable TRUC
$ TRUC=$(cat mon_fichier)
$ echo "$TRUC"
(affiche le texte Í  traiter)
$ RES=$(sed '/^abc|^$/! { p; s|^| -> '"${DOMAIN}"'|g }' "$TRUC")
sed: can't read
(affiche le texte Í  traiter): No such file or directory:
***
Je me dis donc qu'il faut donner Í  sed l'argument qu'il attend, cad un nom de fichier, ce qui n'est pas le cas, car il cherche Í  ouvrir un fichier qui n'existe pas "No such file or directory".
***
Merci
Cordialement
RT
----- Mail original -----
De: "Marc Chantreux"
À: "roger tarani"
Cc: "Liste Debian"
Envoyé: Jeudi 4 Novembre 2021 16:54:56
Objet: Re: Récupérer le résultat d'une commande sed dans une variable ou un fichier : problème...
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
Avatar
roger.tarani
J'ai bien compris l'effet des guillemets/double quotes sur les LF. C'est limpide.
mais je crois que c'est un autre sujet.
Je pense avoir trouvé la solution, comme expliqué plus loin.
Ce que j'ai essayé d'expliquer c'est que:
- si la commande sed prend comme argument un nom de fichier, alors j'obtiens le résultat visé.
- si j'utilise la variable TRUC qui contient la même valeur que le fichier, et bien je n'arrive pas Í  la donner Í  manger Í  la commande sed qui me lance un "No such file or directory" (ce qui suggère qu'lle attend un nom de fichier)
Je reproduis mon exemple, avec les valeurs obtenues.
$ DOMAIN=http://toto.com/
Rq : noter le / final dans mon exemple, et l'utilisation de monfichier
Rq ; ci-dessous j'ai changé et utilisé monfichier
===== CECI FONCTIONNE
$ sed '/^abc|^$/! { p; s|^| -> '"${DOMAIN}"'|g }' monfichier
abc-----
foo
-> http://toto.com/foo
bar
-> http://toto.com/bar
abc-----
baz
-> http://toto.com/baz
$ RES=$(sed '/^abc|^$/! { p; s|^| -> '"${DOMAIN}"'|g }' monfichier)
$ echo "$RES"
abc-----
foo
-> http://toto.com/foo
bar
-> http://toto.com/bar
abc-----
baz
-> http://toto.com/baz
===== MAIS CECI NE FONCTIONNE PAS
A présent, mettons la chaÍ®ne multi-lignes Í  traiter dans une variable TRUC
$ TRUC=$(cat monfichier)
$ echo "$TRUC"
abc-----
foo
bar
abc-----
baz
$ 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 }')
(pas de message d'erreur)
$ echo "$RES"
abc-----
foo
-> http://toto.com/foo
bar
-> http://toto.com/bar
abc-----
baz
-> http://toto.com/baz
Ça marche mais ne nous arrêtons pas lÍ .
Quelle explication rigoureuse peut-on donner de ce phénomène ?
Merci
Cordialement
Roger
----- Mail original -----
De: "Marc Chantreux"
À: "roger tarani"
Cc: "Liste Debian"
Envoyé: Jeudi 4 Novembre 2021 19:06:30
Objet: Re: Récupérer le résultat d'une commande sed dans une variable ou un fichier : problème...
Le Thu, Nov 04, 2021 at 05:18:00PM +0100, 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
Avatar
Marc Chantreux
$ 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
Avatar
roger.tarani
--=_1b34ff74-c7d4-42c7-a8a2-cf5e1d7f7a59
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Oui, en effet, je voulais faire ̓§a : donner la valeur d'une variable ̓  manger ̓  sed.
Et je n'ai m̓ªme pas de fichier mais une variable ̓  traiter.
J'avais cr̓©Íƒ© un fichier pour tenter de d̓©bloquer la situation avec une situation plus connue avec sed.
TRUC est fournie par un traitement pr̓©c̓©dent du script.
Et dans le script, j'obtiens RES ainsi :
RES=$(echo "$TRUC" | sed '/^abc|^$/! { p; s|^| -> '"${DOMAIN}"'|g }')
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
J'ai du mal ̓  discerner la cause exacte.
Est-ce une simple erreur de type ?
Par exemple, l'argument est un fichier qui contient une cha̓®ne de caract̓¨re Vs une cha̓®ne de caract̓¨re d'une variable.
Merci encore
De: "Marc Chantreux"
̓€: "roger tarani"
Cc: "Liste Debian"
Envoy̓©: Jeudi 4 Novembre 2021 22:09:11
Objet: Re: R̓©cup̓©rer le r̓©sultat d'une commande sed dans une variable ou un fichier : probl̓¨me...
$ 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
--=_1b34ff74-c7d4-42c7-a8a2-cf5e1d7f7a59
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div>Oui, en effet, je voulais faire ̓§a : donner la valeur d'une variable ̓  manger ̓  sed.<br>Et je n'ai m̓ªme pas de fichier mais une variable ̓  traiter.<br>J'avais cr̓©Íƒ© un fichier pour tenter de d̓©bloquer la situation avec une situation plus connue avec sed.<br><br>TRUC est fournie par un traitement pr̓©c̓©dent du script.</div><div>Et <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 le script,&nbsp;</span>j'obtiens RES&nbsp; ainsi :<br><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;">RES=$(echo "$TRUC" | sed '/^abc|^$/! { p; s|^| &nbsp;-&gt; '"${DOMAIN}"'|g }')</span><br><br></div><div>Comment expliquer que la commande prend un fichier comme argument :</div><div><span 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;" 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; float: none; display: inline !important;">sed '/^abc|^$/! { p; s|^| &nbsp;-&gt; '"${DOMAIN}"'|g }' monfichier #SUCCES</span><div style="clear: both;" data-mce-style="clear: both;"><br></div><div style="clear: both;" data-mce-style="clear: both;">mais refuse de prendre la valeur d'une variable ?</div><div style="clear: both;" data-mce-style="clear: both;"><span 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;" 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; float: none; display: inline !important;"><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 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;">"$TRUC" #ECHEC</span></span></span><div style="clear: both;" data-mce-style="clear: both;"><br></div><div style="clear: both;" data-mce-style="clear: both;">J'ai du mal ̓  discerner la cause exacte.</div><div style="clear: both;" data-mce-style="clear: both;">Est-ce une simple erreur de type ? </div><div style="clear: both;" data-mce-style="clear: both;">Par exemple, l'argument est un fichier qui contient une&nbsp;<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;">cha̓®ne de caract̓¨re&nbsp;</span>Vs une cha̓®ne de caract̓¨re d'une variable.</div><div style="clear: both;" data-mce-style="clear: both;"><br></div></div></div><div>Merci encore</div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><b>De: </b>"Marc Chantreux" &lt;&gt;<br><b>̓€: </b>"roger tarani" &lt;&gt;<br><b>Cc: </b>"Liste Debian" &lt;&gt;<br><b>Envoy̓©: </b>Jeudi 4 Novembre 2021 22:09:11<br><b>Objet: </b>Re: R̓©cup̓©rer le r̓©sultat d'une commande sed dans une variable ou un fichier : probl̓¨me...<br></div><div><br></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"> $ TRUC=$(cat monfichier)<br>&nbsp;$ RES=$(sed '/^abc|^$/! { p; s|^| &nbsp;-&gt; '"${DOMAIN}"'|g }' &nbsp;"$TRUC")<br><br><br><br>&nbsp;sed: can't read<br>&nbsp;abc-----<br>&nbsp;foo<br>&nbsp;bar<br>&nbsp;<br>&nbsp;abc-----<br>&nbsp;baz<br>&nbsp;: No such file or directory:<br>&nbsp;<br>&nbsp;<br>&nbsp;Idem avec :<br>&nbsp;$ sed '/^abc|^$/! { p; s|^| &nbsp;-&gt; '"${DOMAIN}"'|g }' &nbsp;"$TRUC"<br>&nbsp;<br>&nbsp;<br>&nbsp;***<br>&nbsp;Je me dis donc qu'il faut donner ̓  sed l'argument qu'il attend : cad un nom de fichier.<br>&nbsp;Or, ce n'est pas le cas car je lui donne la valeur d'une variable. <br>&nbsp;Et il me r̓©pond qu'il cherche ̓  ouvrir un fichier qui n'existe pas "No such file or directory".<br><br>&nbsp;Finalement, j'ai tent̓© d'utiliser un pipe ET CA MARCHE !!!<br>&nbsp;RES=$(echo "$TRUC" | sed '/^abc|^$/! { p; s|^| &nbsp;-&gt; '"${DOMAIN}"'|g }')<br><br>ben ... ok j'̓©tais loin de m'imaginer que tu voulais vraiment filer le<br>contenu d'une variable comme argument de sed mais oui en reprenant tes<br>mails: c'̓©tait bien not̓© :(<br><br>sed (comme beaucoup de filtres) une liste de fichiers a traiter en<br>argument (ou alors stdin par defaut)<br><br>au passage du coup:<br><br>TRUC=$( cat monfichier )<br>RES=$(echo "$TRUC" | sed '/^abc|^$/! { p; s|^| &nbsp;-&gt; '"${DOMAIN}"'|g }')<br><br>s'̓©crit en fait<br><br>sed '/^abc|^$/! { p; s|^| &nbsp;-&gt; '"${DOMAIN}"'|g }' monfichier<br><br>ou si tu veux utiliser les redirections pour avoir le nom du fichier<br>devant (c'est plus simple ̓  la relecture)<br><br>&lt; monfichier sed '/^abc|^$/! { p; s|^| &nbsp;-&gt; '"${DOMAIN}"'|g }'<br><br>-- <br>Marc Chantreux<br>Direction du num̓©rique de l'Universit̓© de Strasbourg<br>P̓´le de Calcul et Services Avanc̓©s ̓  la Recherche (CESAR)<br>http://annuaire.unistra.fr/p/20200<br></div></div></body></html>
--=_1b34ff74-c7d4-42c7-a8a2-cf5e1d7f7a59--
Avatar
Charles Plessy
Le Fri, Nov 05, 2021 at 12:31:13AM +0100, 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
Avatar
Marc Chantreux
salut,
Le Fri, Nov 05, 2021 at 12:31:13AM +0100, 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
Avatar
roger.tarani
Bonsoir,
Merci pour ces précisions.
----- Mail original -----
De: "Marc Chantreux"
À: "roger tarani"
Cc: "Liste Debian"
Envoyé: Vendredi 5 Novembre 2021 07:29:45
Objet: Re: Récupérer le résultat d'une commande sed dans une variable ou un fichier : problème...
salut,
Le Fri, Nov 05, 2021 at 12:31:13AM +0100, 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
Avatar
roger.tarani
Bonjour Charles,
Simple et clair.
Merci
cordialement
Rq : parce que TRUC contient des /, j'ai été obligé de prendre | au lieu de / comme séparateur de sed; sinon la commande plante.
----- Mail original -----
De: "Charles Plessy"
À: "Liste Debian"
Envoyé: Vendredi 5 Novembre 2021 01:42:48
Objet: Re: Récupérer le résultat d'une commande sed dans une variable ou un fichier : problème...
Le Fri, Nov 05, 2021 at 12:31:13AM +0100, 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