BASH Script parser to Summarize Your Apache Logs

To summarize Apache logs using a Bash script, you can utilize various command-line tools like awk, grep, and sort. Here’s an example script that summarizes Apache logs by counting the number of occurrences for each unique request path:

#!/bin/bash

logfile="/path/to/apache/access.log"

# Extract the request path from each log line and count occurrences
awk '{print $7}' "$logfile" | grep -oE '^/[^?]+' | sort | uniq -c |
while read -r count path; do
    echo "Path: $path, Count: $count"
done

In this script:

  1. Set the logfile variable to the path of your Apache access log file.
  2. The awk command extracts the request path from each log line. The $7 represents the 7th field, which is typically the request path in Apache log format.
  3. The grep command extracts the path portion, excluding any query parameters, using a regular expression.
  4. The sort command sorts the paths alphabetically.
  5. The uniq -c command counts the occurrences of each unique path.
  6. The while loop reads each line of the output, with count representing the occurrence count and path representing the request path.
  7. Inside the loop, you can perform any additional processing or output formatting as needed. In this example, it simply echoes the path and count.

You can customize the script further based on your specific requirements. For example, you can modify the log file path, add additional log parsing logic, or aggregate data in a different format.

Note: Make sure to replace “/path/to/apache/access.log” with the actual path to your Apache access log file.

Leave a Comment

Your email address will not be published. Required fields are marked *