从文件中删除包含特定字符串的行

时间:2012-08-06 01:40:24

标签: php regex preg-replace line

我需要从日志文件中删除某些字符串。是的,我知道这听起来很落后,但是为了每个人的理智,相信我:没有办法避免这些无用的通知被写入首先登录。

因此,为了使我的日志可读,这些通知必须在写完后删除。

以下是污染日志文件的示例:

ERROR - 2012-08-06 02:12:01 --> Severity: Notice  --> Undefined index: H1 /var/www/vhosts/example.com/httpdocs/application/helpers/mpdf/mpdf.php 14242
ERROR - 2012-08-06 02:12:01 --> Severity: Notice  --> Undefined index: H1>>ID>> /var/www/vhosts/example.com/httpdocs/application/helpers/mpdf/mpdf.php 14288
ERROR - 2012-08-06 02:12:01 --> Severity: Notice  --> Undefined index: H1 /var/www/vhosts/example.com/httpdocs/application/helpers/mpdf/mpdf.php 14242
ERROR - 2012-08-06 02:12:01 --> Severity: Notice  --> Undefined index: H1>>ID>> /var/www/vhosts/example.com/httpdocs/application/helpers/mpdf/mpdf.php 14288
ERROR - 2012-08-06 02:12:01 --> Severity: Notice  --> Undefined index: DIV /var/www/vhosts/example.com/httpdocs/application/helpers/mpdf/mpdf.php 14242

我想删除以下行:

  • 包含'通知'和
  • 包含'未定义'AND
  • 包含'mpdf.php'

我相信这些内容可能是一个开始:

$dir = '/var/www/vhosts/example.com/httpdocs/application/logs/log-2012-07-07.php';

$content = file_get_contents($dir);

$new = preg_replace($pattern, '', $content) // <===== need help here with $pattern regex

file_put_contents($dir, $new);

您对如何做到这一点有什么建议吗?

1 个答案:

答案 0 :(得分:1)

sed -i '/Notice/ { /Undefined/ { /mpdf.php/ d}}' LOG_FILE

更一般,

A="..." B="..." C="..." sed -i "/$A/{/$B/{/$C/ d}}" FILE

将删除包含字符串$ A和$ B以及$ C的所有行。