如何正确使用sed从变量中搜索/替换部分行

时间:2021-09-11 08:43:25

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.

背景:我正在使用ZenCart php软件,用sed我想编辑不同的主题,但使用相同的php文件。所以通过sed对主题进行一些自动更改。

s1=$'<img itemprop="image" src="'\'' . zen_output_string($src) . '\''"'   
sed -i.bak -r 's/$image = '\''<img src="'\'' \. zen_output_string\($src\) \. '\''" alt="'\'' \. zen_output_string\($alt\) \. '\''"'\'';/'"$s1"/ html_output.php

This runs ok, but no changes are in the file, why, what's wrong?

运行正常,但文件中没有任何更改,为什么,出错了什么?

Edit I managed to workaround this problem, resulting in the following script:

编辑我设法解决此问题,导致以下脚本:

echo "Updating your Zencart theme's files..."
echo "Creating backups of important files."
mkdir -p ./zentmp/includes/functions && cp ./includes/functions/html_output.php ./zentmp/includes/functions/ && mkdir -p ./zentmp/includes/templates && cp -Rf ./includes/templates/ ./zentmp/includes/templates && mkdir -p ./zentmp/includes/classes && 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

The above works pretty well, until the following piece of code:

以上工作非常好,直到下面的代码:

for dir2 in ./zentmp/includes/templates/*/
do
dir2=${dir2%*/}
s4=$"<span itemprop="name"><?php echo \$products_name; ?>"
sed -i -r 's/<?php echo \$products_name; ?>'/'"${s4}"/ ./$dir2/tpl_product_info_display.php
done

As requested, I'll post the snippets I'm trying to replace in order of usage:

根据要求,我将按照使用顺序发布我要替换的片段:

$image = '<img src="' . zen_output_string($src) . '" alt="' . zen_output_string($alt) . '"';

to

$image = '<img itemprop="image" src="' . zen_output_string($src) . '" alt="' . zen_output_string($alt) . '"';

Second:

<span class="imgLink">

to

<span  itemprop="image" class="imgLink">

Third:

$rel . '" title="' . addslashes($products_name) . '">

to

$rel . '" title="' . addslashes($products_name) . '" itemprop="image">

Note that the 2nd sed is used twice.

请注意,第二个sed使用了两次。

1 个解决方案

#1


1  

sed -r -i.bak --posix "/$image = '<img src=/ s/<img src=/<img itemprop=\"image\" src=/
/<span class=/ s/class=/itemprop=\"image\" class=/
/\\$rel.*title.*addslashes/ s/> *$/ itemprop=\"image\">/
" sample.txt

3 section, eache searching and limited line with expected pattern, than replace just part needed inside (avoiding lot of " and ' conversion by shell)

3节,eache搜索和限制线与预期模式,而不是替换内部所需的部分(避免很多“和'转换为shell)

\" arround image are mandatory for simplification (could use a back pattern like \1 if capture of the " are made in search patterne of s/ \$ is necessary on the last searche due to shell and sed substitution

\“arround图像对于简化是强制性的(如果由于shell和sed替换而在最后一个搜索中搜索s / \ $的搜索模式中的”捕获“,则可以使用像\ 1这样的后向模式

--posix is there to allow reuse of the same sed code in non GNU sed

--posix是允许在非GNU sed中重用相同的sed代码

#1


1  

sed -r -i.bak --posix "/$image = '<img src=/ s/<img src=/<img itemprop=\"image\" src=/
/<span class=/ s/class=/itemprop=\"image\" class=/
/\\$rel.*title.*addslashes/ s/> *$/ itemprop=\"image\">/
" sample.txt

3 section, eache searching and limited line with expected pattern, than replace just part needed inside (avoiding lot of " and ' conversion by shell)

3节,eache搜索和限制线与预期模式,而不是替换内部所需的部分(避免很多“和'转换为shell)

\" arround image are mandatory for simplification (could use a back pattern like \1 if capture of the " are made in search patterne of s/ \$ is necessary on the last searche due to shell and sed substitution

\“arround图像对于简化是强制性的(如果由于shell和sed替换而在最后一个搜索中搜索s / \ $的搜索模式中的”捕获“,则可以使用像\ 1这样的后向模式

--posix is there to allow reuse of the same sed code in non GNU sed

--posix是允许在非GNU sed中重用相同的sed代码