To summarize NGINX logs using a Bash script, you can utilize various command-line tools like awk
, grep
, and sort
. Here’s an example script that summarizes NGINX logs by counting the number of occurrences for each unique request path:
#!/bin/bash
logfile="/path/to/nginx/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:
- Set the
logfile
variable to the path of your NGINX access log file. - The
awk
command extracts the request path from each log line. The$7
represents the 7th field, which is typically the request path in NGINX log format. - The
grep
command extracts the path portion, excluding any query parameters, using a regular expression. - The
sort
command sorts the paths alphabetically. - The
uniq -c
command counts the occurrences of each unique path. - The
while
loop reads each line of the output, withcount
representing the occurrence count andpath
representing the request path. - 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/nginx/access.log” with the actual path to your NGINX access log file.