2015-08-07 2 views
0

Как сделать sed с использованием переменной с escape-символами?с использованием sed с escape-символами в переменной

Это мой файл:

$ 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> 

Я пытаюсь WITE следующей команды (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> 

в сценарии с использованием переменных, но escape-символы или другие становятся лучше меня:

Это моя попытка моего сценария, где я могу получить 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\---------------" 

----------- Edit1 ниже дальнейшего объяснения и/или для моей ссылки -----------------


Это файл, я хочу, чтобы СЭД на:

$ 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>

так возможно это что-то делать с моими спасательными персонажами?

+3

Используйте двойные кавычки для расширения переменных: 'sed 's/$ s1/123/g" ' – anubhava

+3

^^ Плюс, так как ваши шаблоны, скорее всего, будут tain '/', вам нужно будет использовать другой разделитель (например, '|') для sed: например. 'sed 's | $ s1 | 123 | g" ' – anishsane

+0

Полезно знать. Я использовал одиночные кавычки '' ', поскольку у меня есть двойные кавычки' '' в моей переменной. И я попытался использовать '|', но это не полностью работает – HattrickNZ

ответ

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> 
Смежные вопросы