简体   繁体   English

Perl或Sed命令在匹配表达式之前删除内容

[英]Perl or Sed command to delete content before matching expression

I have a directory of html files in which I'd like to remove all content (and including) a matched expression. 我有一个html文件目录,我想在其中删除所有内容(包括匹配的表达式)。 I have tried this (where XXXXXX us the string I am looking for: 我已经尝试过此操作(其中XXXXXX是我们要查找的字符串:

perl -pi -e "$a++if s/.*XXXXXX.*//si;$a||s/.*//s" *.html

I am getting this error (where when I press enter, it is changing my command, see first line): 我收到此错误(当我按Enter时,它正在更改命令,请参阅第一行):

perl -pi -e "$a=1 if *.htmla && s/.*XXXXXX.*//is; s/.*//s if *.htmla" *.html
Bareword found where operator expected at -e line 1, near "*.htmla"
    (Missing operator before htmla?)
Bareword found where operator expected at -e line 1, near "*.htmla"
    (Missing operator before htmla?)
syntax error at -e line 1, near "="
syntax error at -e line 1, near "*.htmla
"
Execution of -e aborted due to compilation errors.

Any help on Perl command to use? 对Perl命令有帮助吗?

Try using 尝试使用

'

instead of 代替

"

otherwise your shell will expand $a. 否则您的外壳将扩展$ a。

perl -pi -e "$a++if s/.*XXXXXX.*//si;$a||s/.*//s" *.html

should do the trick. 应该可以。

If you want to delete all lines up to a line, which matches some regular expression. 如果要删除所有与某条正则表达式匹配的行。

You can give a range to sed and delete from the first line until and including the match 您可以给sed一个范围,并从第一行开始删除,直到包括该匹配项为止

sed -i -e '1,/match/d' file.html

If you want to remove the content from every line, which matches a regular expression 如果要从与正则表达式匹配的每一行中删除内容

sed -i -e 's/.*match//' file.html

Quite with ' instead of " . 相当用'代替"

Variables are interpolated in " -quoted material; shells do this and so does Perl. 变量插在" -quoted材料;贝壳做到这一点,这样做的Perl。

In this case, your shell which will substitute $a with its value before feeding the result to Perl. 在这种情况下,您的外壳程序会将$a替换为其值,然后再将结果提供给Perl。 The result is illegal Perl syntax. 结果是非法的Perl语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM