Last Updated on 3 months by Sachin G

In this post, I’ll explore how to filter Apache logs by date using the powerful “sed” editor, offering a versatile solution for extracting valuable information from your log files. Apache logs are valuable resources for server administrators, providing insights into server activities and visitor interactions. However, analyzing logs for specific time frames can be challenging.

Understanding the Challenge

Here is this article when I worked on a web server or any site issue or unusual activity the Apache log file is only my friend but it is very complicated to read because it has so many entries like date time activity IP, messages, user ID, etc. There are so many tools that allow us to generate reports of Apache that visitors on the sites and their activity, what they are doing. Only Apache log files can tell us, all activity on the site. This is where the “sed” (stream editor) comes to the rescue, offering a robust method to filter logs based on dates.

Utilizing Sed for Date-Based Filtering

Find the Apache Log File

The first step is to identify the location of your Apache log file. Common file paths include /var/log/apache2/access.log or /var/log/httpd/access_log. Use the appropriate path based on your server configuration. Here we use sed tools which will fetch Apache logs by date. I am going to explain the combined log format, which will help us to read and be comfortable to use for managing activity. If we see our Apache combined log, we found entries like below.

100.83.233.67- - [23/May/2004:11:00:48 +1000] "GET /robots.txt HTTP/1.0" 200 468 "-" "elbhealthcheck/2.1"

100.83.233.67- - [23/May/2004:11:0048 +1000] "GET / HTTP/1.0" 200 6433 "-" "elbhealthcheck/2.1"

LogFormat :

%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i"


Whereas : 

%h   =  IP address of the client

%l   =  RFC 1413 identity of the client

%u   =  user-id of the person

%t   =  Finish time request processing.

%r   =  request line from the client in double quotes

%>s  =  status code that the server sends back to the client

%b   =  Size of the object returned to the client

Refer to and User agent source of the request originated and those agents who have made the request.

Formulating the Sed Command

Now, let’s construct a “sed” command to filter logs for a specific date range. Suppose we want logs for January 1, 2014. The command looks like this:

# sed -n '/1\/Jan\/2014/,$p' /path/to/access_log

The above command will show lines (p) starting with 1/Jan/2014 through the end of the log files ($)

Let’s break down the command:

-n: Suppresses automatic printing of pattern space, allowing us to control the output.

/01\/Jan\/2014/,$p: Specify the range of lines to print, starting from the line containing ’01/Jan/2014′ until the end of the file.

Filtering by Date Range

If we need to log only by date range, assume from 1/Jan/2014 to 3/Jan/2014, this will be almost the same, instead of fetching the last lines through the end of the file, You could try something like this.

# sed -n '/1\/Jan\/2014/,/3\/Jan\/2014/ p /path/to/your/access.log'

Filtering Apache logs by date using the “sed” editor is like using a powerful tool to find exactly what you need in a sea of information. It’s useful when you’re trying to fix problems, understand how people are using your website, or make sure everything is secure. Learning how to do this helps you go through logs more easily and find the details you’re looking for with accuracy.