>_ IzzyLab

Hands-on task

This task is step 9 of the course “Text in the terminal: grep, sed, awk”

IP allowlist from access requests

hard~10 min

Colleagues sent access requests in /root/access_requests.txt — the text contains IP addresses, and not all of them are real: someone made a typo and wrote an octet greater than 255. Collect into /root/allowlist.txt only valid IPv4 addresses (each octet 0–255) — one per line, no duplicates.

What you'll practise:
  • grep -oE
  • alternation
  • ipv4
  • word boundaries
  • sort -u

Topic breakdown

A "one to three digits" pattern for an octet lets 300 and 999 through: a regex doesn't understand numeric ranges, it only sees characters. So the 0–255 range is split into non-overlapping pieces by digit position and glued together with alternation. The second trap is a partial match: without boundaries you can grab a valid-looking 56.1.1.1 out of 256.1.1.1. The word boundary \b requires that there's no digit before or after the address, and cuts off such fragments.

What's checked

  1. valid addresses collected
  2. invalid addresses and their fragments filtered out
  3. addresses without duplicates

What it looks like