Skip to content

Instantly share code, notes, and snippets.

@raecoo
Last active September 21, 2023 07:12
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save raecoo/6610f860d39cd73dfb2b to your computer and use it in GitHub Desktop.
Save raecoo/6610f860d39cd73dfb2b to your computer and use it in GitHub Desktop.
awk/grep commands for Rails log analysis
# Access number
cat production.log | grep "^Processing" | wc | awk '{print $1}'
# Each IP access number
cat production.log | grep “^Processing” | awk ‘{print $4}’ | uniq -c
# Independent IP number
cat production.log | grep "^Processing" | awk '{print $4}' | uniq | wc | awk '{print $1}'
cat production.log | grep “^Processing” | awk ‘{print $4}’ | uniq | wc -l
# Account number for each pages
cat production.log |grep “200 OK” | awk ‘{print $17}’ | sort | uniq -c
# Exception info
cat production.log | grep "Error" -6 -n > error.log
# Send exception info to specify email
cat production.log | grep "Error" -6 -n | mail -s "Error Logs" recipient@domain.com
# Time-consuming actions
cat production.log | grep "200 OK" | awk '{print $17}'| sort | uniq -c | sort -r -n | head -n 500 > stat.log
# Top 200 IPs
grep Processing production.log | awk '{print $4}' | sort | uniq -c | sort -r -n | head -n 200 > top-200-ip.log
@raecoo
Copy link
Author

raecoo commented Dec 10, 2015

find the index where you want to start printing (index of $5 in $0) and print the substring of $0 starting at that index.
awk '{print substr($0,index($0,$5))}'
E.g
I, [2015-12-10T03:19:02.378362 #12408] INFO -- : Completed 200 OK in 21ms (Views: 3.3ms | ActiveRecord: 0.8ms)
result
21ms (Views: 3.3ms | ActiveRecord: 0.8ms)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment