>_ IzzyLab

Hands-on task

This task is step 13 of the course “sed: the stream editor”

sed: mask phone numbers in a support log

medium~7 min

The support log /root/support.log goes to a contractor, and it contains customers' phone numbers like +7 912 345-67-89. Mask every digit of each number except the last two with asterisks — +7 *** ***-**-89 (keep the +7 country code). A line may contain two numbers. Write the result to /root/support.masked.log.

What you'll practise:
  • sed -E
  • capture groups
  • {n} counter
  • data masking
  • g flag

Topic breakdown

Masking is a substitution where the pattern describes the structure of the whole value, and only what must be kept goes in parentheses: it comes back into the replacement as a backreference, and the rest is rewritten as a literal. The exact number of digits is set by the {n} counter — that way the pattern won't catch an order number or an amount. Characters that mean something in a regex (here +) are escaped. The g flag handles every match in the line, not just the first.

What's checked

  1. numbers hidden
  2. mask is correct, the rest of the text in place

What it looks like