Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I correctly use sed to search/replace parts of a line from a variable
    text
    copied!<p>Background: I am using ZenCart php software, with sed I wanted to edit different themes, but with the same php files. So some automatic changes to themes via sed. </p> <pre><code>s1=$'&lt;img itemprop="image" src="'\'' . zen_output_string($src) . '\''"' sed -i.bak -r 's/$image = '\''&lt;img src="'\'' \. zen_output_string\($src\) \. '\''" alt="'\'' \. zen_output_string\($alt\) \. '\''"'\'';/'"$s1"/ html_output.php </code></pre> <p>This runs ok, but no changes are in the file, why, what's wrong?</p> <p><strong>Edit I managed to workaround this problem, resulting in the following script:</strong></p> <pre><code>echo "Updating your Zencart theme's files..." echo "Creating backups of important files." mkdir -p ./zentmp/includes/functions &amp;&amp; cp ./includes/functions/html_output.php ./zentmp/includes/functions/ &amp;&amp; mkdir -p ./zentmp/includes/templates &amp;&amp; cp -Rf ./includes/templates/ ./zentmp/includes/templates &amp;&amp; mkdir -p ./zentmp/includes/classes &amp;&amp; cp ./includes/classes/breadcrumb.php /zentmp/includes/classes/ echo "Updating theme file /includes/functions/html_output.php" s1=$'itemprop="image" src="'\'' . zen_output_string($src) . '\''"' sed -i -r 's/src="'\'' \. zen_output_string\(\$src\) \. '\''"/'"${s1}"/ html_output.php echo "File updated..." s2=$'itemprop="image" class="imgLink"' for dir in ./zentmp/includes/templates/*/ do dir=${dir%*/} echo "Updating theme file /$dir/tpl_modules_main_product_image.php" (1/2) sed -i -r 's/class="imgLink"/'"${s2}"/ ./$dir/tpl_modules_main_product_image.php echo "Updating theme file /$dir/tpl_modules_main_product_image.php" (2/2) s3=$'title="'\'' . addslashes($products_name) . '\'' itemprop="image"' sed -i -r 's/title="'\'' \. addslashes\(\$products\_name\) \. '\''"/'"${s3}"/ ./$dir/tpl_modules_main_product_image.php done </code></pre> <p>The above works pretty well, until the following piece of code:</p> <pre><code>for dir2 in ./zentmp/includes/templates/*/ do dir2=${dir2%*/} s4=$"&lt;span itemprop="name"&gt;&lt;?php echo \$products_name; ?&gt;" sed -i -r 's/&lt;?php echo \$products_name; ?&gt;'/'"${s4}"/ ./$dir2/tpl_product_info_display.php done </code></pre> <p>As requested, I'll post the snippets I'm trying to replace in order of usage:</p> <blockquote> <pre><code>$image = '&lt;img src="' . zen_output_string($src) . '" alt="' . zen_output_string($alt) . '"'; </code></pre> <p>to</p> <pre><code>$image = '&lt;img itemprop="image" src="' . zen_output_string($src) . '" alt="' . zen_output_string($alt) . '"'; </code></pre> </blockquote> <p>Second:</p> <blockquote> <pre><code>&lt;span class="imgLink"&gt; </code></pre> <p>to</p> <pre><code>&lt;span itemprop="image" class="imgLink"&gt; </code></pre> </blockquote> <p>Third:</p> <pre><code>$rel . '" title="' . addslashes($products_name) . '"&gt; </code></pre> <p>to</p> <pre><code>$rel . '" title="' . addslashes($products_name) . '" itemprop="image"&gt; </code></pre> <p>Note that the 2nd sed is used twice.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload