为什么我的perl替换不起作用? [英] Why is my perl replace not working?

查看:230
本文介绍了为什么我的perl替换不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$> 

c $ c> perl -pi -w -e's / onclick = \img = document.getElementById(\'img_1\')\; img.style.display =(img.style.display == ''''''''''''''''''''''''';''''); return false//'test.html

  perl -pi -w -e's / b 

onclick =img = document.getElementById('img_1'); img.style.display =(img.style.display =='none'?'block':'none'); return false//'test.html

我的目标是取代 onclick =img = document.getElementById(' img_1'); img.style.display =(img.style.display =='none'?'block':'none'); return false文件中没有任何内容的test.html 。我在搞什么?



我一直在得到错误: -sh:在意外标记附近出现语法错误)``我不禁但感觉是因为我的一些愚蠢的逃避。请帮助我。

解决方案

[您没有指定您正在构建命令的shell。我将假设 sh bash 。 ]



问题#1:将文本转换为正则表达式模式

很多字符在正则表达式中有特殊的含义。在你使用的那些中,是特别的。



如果您想匹配

  onclick =img = document.getElementById('img_1'); img.style.display =(img.style.display =='none'?'block':'none'); return false

您需要使用模式

  onclick =img = document\.getElementById\('img_1'\); img\.style\.display = \(img\.style\.display =='none' \'block':'none'\); return false



  s / onclick =img = document\.getElementById\('img_1'\); img\ .style \.display = \(img\.style\.display =='none'\?'block':'none'\); return false// 






问题2:将文本(Perl程序)转换为shell文本 h2>

这是您询问的问题。



由单引号(')引用的部分在下一个单引号(无条件地)结束,因此以下错误:

  echo'foo\\'''bar'#第三个''不匹配。 

如果您想输出 foo'bar ,您可以使用

  echo'foo'\\''bar'#foo,'的连接和酒吧。 

foo bar 用单引号引用,单引号使用 \ 进行转义。 \ 在这里工作,因为它在单引号之外。

所以,课程基本上使用'\''而不是'在单引号内。



你想把下面的程序传给Perl:

  s / onclick =img = document\.getElementById\('img_1 '\); img\.style\.display = \(img\.style\.display =='none'\?'block':'none'\); return false/ / 

为此,您需要创建一个shell文字来产生正确的参数,意思是我们希望用单引号括起整个单词,并且避开现有的单引号。我们得到以下结果:

 's / onclick =img = document \.getElementById\('\'' img_1'\''\); img\.style\.display = \(img\.style\.display =='\''none'\''\?'\\ ''''''''''''''); return false//'

因此,整个命令将是

  perl -i -wpe's / onclick = img = document \.getElementById\('\''img_1'\''\); img\.style\.display = \(img\.style\.display == '\\'''''''''''''''''''''''''':'\'''); return false//'test.html 


I have the following perl search and replace :

With Escapes :

perl -pi -w -e 's/ onclick=\"img=document.getElementById(\'img_1\')\; img.style.display = (img.style.display == \'none\' ? \'block\' : \'none\');return false"//' test.html 

Without Escapes :

perl -pi -w -e 's/ onclick="img=document.getElementById('img_1'); img.style.display = (img.style.display == 'none' ? 'block' : 'none');return false"//' test.html 

My objective is to replace onclick="img=document.getElementById('img_1'); img.style.display = (img.style.display == 'none' ? 'block' : 'none');return false" with nothing in the file test.html. What am I messing up?

I keep on getting the error : -sh: syntax error near unexpected token)'` which I cannot help but feel is because of some stupid escaping on my part. Please help me out.

解决方案

[ You didn't specify for which shell you are building the command. I'm going to assume sh or bash. ]


Problem #1: Converting text into a regex pattern

Many characters have a special meaning in regex patterns. Among those you used, (, ), . and ? are special. These need to be escaped.

If you want to match

onclick="img=document.getElementById('img_1'); img.style.display = (img.style.display == 'none' ? 'block' : 'none');return false"

You need to use the pattern

onclick="img=document\.getElementById\('img_1'\); img\.style\.display = \(img\.style\.display == 'none' \? 'block' : 'none'\);return false"

So your Perl code is

s/onclick="img=document\.getElementById\('img_1'\); img\.style\.display = \(img\.style\.display == 'none' \? 'block' : 'none'\);return false"//


Problem #2: Converting text (a Perl program) into a shell literal

This is the problem you asked about.

Sections quoted by single quotes ("'") end at the next single quote (unconditionally), so the following is wrong:

echo 'foo\'bar'   # Third "'" isn't matched.

If you wanted to output "foo'bar", you could use

echo 'foo'\''bar'   # Concatenation of "foo", "'" and "bar".

foo and bar are quoted by single quotes, and the single quote is escaped using \. \ works here because it's outside of single quotes.

So, the lesson is basically use '\'' instead of ' inside single quotes.

you want to pass the following program to Perl:

s/onclick="img=document\.getElementById\('img_1'\); img\.style\.display = \(img\.style\.display == 'none' \? 'block' : 'none'\);return false"//

To do so, you'll need to create a shell literal that produces the correct argument, meaning we want to surround the whole with single quotes and escape the existing single quotes. We get the following:

's/onclick="img=document\.getElementById\('\''img_1'\''\); img\.style\.display = \(img\.style\.display == '\''none'\'' \? '\''block'\'' : '\''none'\''\);return false"//'

As such, the entire command would be

perl -i -wpe's/onclick="img=document\.getElementById\('\''img_1'\''\); img\.style\.display = \(img\.style\.display == '\''none'\'' \? '\''block'\'' : '\''none'\''\);return false"//' test.html

这篇关于为什么我的perl替换不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆