在变量中使用带有转义字符的sed

时间:2015-08-07 03:52:23

标签: regex bash sed

如何使用带转义字符的变量进行sed

这是我的档案:

$ cat file1.txt
abcdefg
<p class="text-muted">&copy; 2014. Core Team</p>


some more text


<p class="text-muted">&copy; 2014. Core Team</p>


some more text

</head>

我试图使用以下命令(whcih在命令行中使用转义字符工作)

$ sed 's/<p class="text-muted">&copy; 2014. Core Team<\/p>/99999/g' file1.txt
abcdefg
99999


some more text


99999


some more text

</head>

在使用变量的脚本中,但转义字符或其他字符对我来说越来越好:

这是我在我的脚本中的尝试,我可以grep使用变量工作,但我无法使sed工作。谁能帮我?

#!/bin/bash
###
# this is the text i want to find 
# <p class="text-muted">&copy; 2014. Core Team</p>
# and this is the text I want to replace depending on the year
# <p class="text-muted">&copy; 2015. Core Team</p>

echo "hello this is a script to find a string and replace it with a string"

#s1='<p class="text-muted">&copy; 2014. Core Team</p>'  without escape characters
s1='<p class="text-muted">&copy; 2014. Core Team</p>'
s2='<p class="text-muted">&copy; 2015. Core Team</p>'

echo "s1="
echo "$s1"

echo "s2="
echo "$s2"


echo -e "\n\n\n\n\n\n\----------------\n\n\n\n\n\---------------"

grep "$s1" file1.txt #> file2.txt

sed 's/$s1/123/g' file1.txt #> file2.txt
#cat file2.txt


echo -e "\n\n\n\n\n\n\----------------\n\n\n\n\n\---------------"

-----------以下编辑1 - 进一步说明和/或供我参考----------------- < / p>

这是我要发布的文件:

$ cat file1.txt
abcdefg
123
<p class="text-muted">&copy; 2014. Core Team</p>


some more text


<p class="text-muted">&copy; 2014. Core Team</p>


some more text

</head>

这是我的剧本

$ cat insertText2.sh
#!/bin/bash
###
# this is the text i want to find
# <p class="text-muted">&copy; 2014. Core Team</p>
# and this is the text I want to replace depending on the year
# <p class="text-muted">&copy; 2015. Core Team</p>

echo "hello this is a script to find a string and replace it with a string"

#s1='<p class="text-muted">&copy; 2014. Core Team</p>'  without escape characters
s1='<p class="text-muted">&copy; 2014. Core Team</p>'
s2='<p class="text-muted">&copy; 2015. Core Team</p>'

echo "s1="
echo "$s1"

echo "s2="
echo "$s2"


echo -e "\n\n-------grep on file1.txt--\n\n---------------"

grep "$s1" file1.txt #> file2.txt

echo -e "\n\n-------cat on file1.txt--\n\n---------------"

cat file1.txt

echo -e "\n\n-------sed on file1.txt--\n\n---------------"

#sed "s/$s1/123/g" file1.txt #> file2.txt
#sed "s|$s1|123|g" file1.txt #> file2.txt
sed "s|$s1|$s2|g" file1.txt #> file2.txt
#sed 's/123/ABC/g' file1.txt #> file2.txt
#cat file2.txt

这是我的脚本的输出

$ ./insertText2.sh file1.txt
hello this is a script to find a string and replace it with a string
s1=
<p class="text-muted">&copy; 2014. Core Team</p>
s2=
<p class="text-muted">&copy; 2015. Core Team</p>


-------grep on file1.txt--

---------------
<p class="text-muted">&copy; 2014. Core Team</p>
<p class="text-muted">&copy; 2014. Core Team</p>


-------cat on file1.txt--

---------------
abcdefg
123
<p class="text-muted">&copy; 2014. Core Team</p>


some more text


<p class="text-muted">&copy; 2014. Core Team</p>


some more text

</head>


-------sed on file1.txt--

---------------
abcdefg
123
<p class="text-muted"><p class="text-muted">&copy; 2014. Core Team</p>copy; 2015. Core Team</p>


some more text


<p class="text-muted"><p class="text-muted">&copy; 2014. Core Team</p>copy; 2015. Core Team</p>


some more text

</head>

但这不是我想做的,而是取代了 <p class="text-muted">&copy; 2014. Core Team</p>

<p class="text-muted"><p class="text-muted">&copy; 2014. Core Team</p>copy; 2015. Core Team</p>

所以也许这与我的逃脱角色有关?

1 个答案:

答案 0 :(得分:0)

我想出的解决方案就是这个 sed "s|$s1|$s2|g" file1.txt我必须在变量中的&符\上使用转义字符&

这是我正在搜索的文件:

$ cat file1.txt
abcdefg
123
<p class="text-muted">&copy; 2014. Core Team</p>
<p basic2014="test">&copy; 2014. Core Team</p>
some more text


<p class="text-muted">&copy; 2014. Core Team</p>
<p basic2014="test">&copy; 2014. Core Team</p>

some more text

</head>

这是我正在运行的脚本:

$ cat insertText3.sh
#!/bin/bash
###
# this is the text i want to find
# <p class="text-muted">&copy; 2014. Core Team</p>
# and this is the text I want to replace depending on the year
# <p class="text-muted">&copy; 2015. Core Team</p>

echo -e "hello this is a script to find a string and replace it with a string:\n"

#s1='<p class="text-muted">&copy; 2014. Core Team</p>'  without escape characters ## what I want to find and replace
s1='<p class="text-muted">\&copy; 2014. Core Team</p>' ## need the single quotes here
s2='<p class="text-muted">\&copy; 2015. Core Team</p>'

echo "this is what I want to find (note the escape character(\) in the variable to escape the ampersand(&) )(s1)="
echo "$s1"
echo -e "\n"

echo "this is what I want it to be replaced with (s2)="
echo "$s2"


echo -e "\n\n-------grep on file1.txt--\n\n---------------"

grep "$s1" file1.txt #> file2.txt

echo -e "\n\n-------cat on file1.txt--\n\n---------------"

cat file1.txt

echo -e "\n\n-------sed on file1.txt--\n\n---------------"


sed "s|$s1|$s2|g" file1.txt #> file2.txt   works ## double quotes gives an error, single quotes runs with no errrors but does not alter the text
                                                ## because I have forward/backslash in the variable i use |

# #sed -i.bak2 "$c2" *.html

这是脚本运行的输出:

$ ./insertText3.sh
hello this is a script to find a string and replace it with a string:

this is what I want to find (note the escape character(\) in the variable to escape the ampersand(&) )(s1)=
<p class="text-muted">\&copy; 2014. Core Team</p>


this is what I want it to be replaced with (s2)=
<p class="text-muted">\&copy; 2015. Core Team</p>


-------grep on file1.txt--

---------------
<p class="text-muted">&copy; 2014. Core Team</p>
<p class="text-muted">&copy; 2014. Core Team</p>


-------cat on file1.txt--

---------------
abcdefg
123
<p class="text-muted">&copy; 2014. Core Team</p>
<p basic2014="test">&copy; 2014. Core Team</p>
some more text


<p class="text-muted">&copy; 2014. Core Team</p>
<p basic2014="test">&copy; 2014. Core Team</p>

some more text

</head>


-------sed on file1.txt--

---------------
abcdefg
123
<p class="text-muted">&copy; 2015. Core Team</p> ##<< 2014 has changed to 2015
<p basic2014="test">&copy; 2014. Core Team</p>
some more text


<p class="text-muted">&copy; 2015. Core Team</p> ##<< 2014 has changed to 2015
<p basic2014="test">&copy; 2014. Core Team</p>

some more text

</head>