Read more

Bash: How to count and sort requests by IP from the access logs

Emanuel
January 23, 2024Software engineer at makandra GmbH

Example

87.140.79.42 - - [23/Jan/2024:09:00:46 +0100] "GET /monitoring/pings/ HTTP/1.1" 200 814 "-" "Ruby"
87.140.79.42 - - [23/Jan/2024:09:00:46 +0100] "GET /monitoring/pings/ HTTP/1.1" 200 814 "-" "Ruby"
87.140.79.41 - - [23/Jan/2024:09:00:46 +0100] "GET /monitoring/pings/ HTTP/1.1" 200 814 "-" "Ruby"
87.140.79.42 - - [23/Jan/2024:09:00:46 +0100] "GET /monitoring/pings/ HTTP/1.1" 200 814 "-" "Ruby"

Goal

Count and sort the number of requests for a single IP address.

Bash Command

awk '{ print $1}' test.log | sort | uniq --count

Result

1 87.140.79.41
3 87.140.79.42

Explain

  1. awk '{ print $1}' test.log
87.140.79.42
87.140.79.42
87.140.79.41
87.140.79.42
  1. sort
87.140.79.41
87.140.79.42
87.140.79.42
87.140.79.42
  1. uniq --count [1]
1 87.140.79.41
3 87.140.79.42
  • [1] -c, --count: prefix lines by the number of occurrence
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot
Posted by Emanuel to makandra dev (2024-01-23 08:59)