>_ IzzyLab

Hands-on task

This task is step 20 of the course “Regular expressions”

Extract IPv4 strictly

hard~10 min

From /root/access.txt extract IPv4 addresses, but only valid ones — each octet from 0 to 255 (the log also contains junk "addresses" like 999.999.999.999). Write the result to /root/valid_ips.txt, one address per line, without duplicates.

What you'll practise:
  • grep
  • regex
  • alternation
  • ipv4

Topic breakdown

A naive "one-to-three digits" pattern for an octet lets junk like 999 or 256 through — the 0–255 range can't be described with a single character class. The solution is an alternation | of several non-overlapping subranges, where separate places are fixed for the higher values and an optional place is set with ?. Four such octets are glued together with escaped dots, and grep -Eo prints only the matched addresses; uniqueness is added by a separate sort pass.

What's checked

  1. valid IPs extracted
  2. invalid addresses filtered out
  3. IPs are unique

What it looks like