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
logfilevariable to the path of your NGINX access log file. - The
awkcommand extracts the request path from each log line. The$7represents the 7th field, which is typically the request path in NGINX log format. - The
grepcommand extracts the path portion, excluding any query parameters, using a regular expression. - The
sortcommand sorts the paths alphabetically. - The
uniq -ccommand counts the occurrences of each unique path. - The
whileloop reads each line of the output, withcountrepresenting the occurrence count andpathrepresenting 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.