Michael Samuel
Michael Samuel

Reputation: 3920

Read error log for a certain day

I have a large 1GB error log called "error_log" and I need to read the errors from a certain day only. I have this simple PHP code to achieve that:

<?php

$lines = `cat error_log | grep '[27-Aug'`;
echo $lines;

?>

For some reason this code outputs nothing when I tried it. I want to read errors that were logged on day 27 August so I use [27-Aug' as it's the prefix for each entry.

Is there something wrong in the above code? I'm sure of the file path as I tried the head command and it worked well.

Upvotes: 0

Views: 98

Answers (1)

Jotne
Jotne

Reputation: 41450

This is wrong

grep '[27-Aug'

try

grep '\[27-Aug'

[ is a special character and needs to be escaped

But change line to:

lines=$(grep '\[27-Aug' error_log)

Do not cat to file to program that can read it itself.
Avoid using old and outdated back-tics, use parentheses like this var=$(code)

Upvotes: 1

Related Questions