Skip to content

Instantly share code, notes, and snippets.

@wesley-dean
Last active April 20, 2020 15:19
Show Gist options
  • Save wesley-dean/8c94aa164cb9c21957c5d7ec8657ef01 to your computer and use it in GitHub Desktop.
Save wesley-dean/8c94aa164cb9c21957c5d7ec8657ef01 to your computer and use it in GitHub Desktop.
basic, generic sed script to sanitize things like AWS account numbers, IPv4 addresses, SSNs, TINs, etc. from text files, program output, etc.
#!/usr/bin/env -S sed -Ef
# Replace 12-digit strings (AWS Account IDs)
# 123456789012 => 123*********
s/([^0-9])([0-9]{3})[0-9]{9}([^0-9])/\1\2*********\3/g
# Replace Social Security Numbers (SSNs) with dashes =>
# 123-45-6789 => 123-**-****
s/([^0-9])([0-9]{3})-([0-9]{2})-([0-9]{4})([^0-9])/\1\2-**-****\5/g
# Replace Taxpayer ID Numbers (TINs) containing dashes
# 12-3456789 => 12-*******
s/([^0-9])([0-9]{2})-([0-9]{2})[0-9]{5}([^0-9])/\1\2-\3*****\4/g
# Replace SSNs / TINs not containing dashes
# 123456789 => 123******
s/(["'])([0-9]{3})([0-9]{9})\1/\1\2******\1/g
# Replace credit card numbers containing dashes
# 1234-5555-6666-7777 => 1234-****-****-****
s/([^0-9]*)([0-9]{4})-([0-9]{4})-([0-9]{4})-([0-9]{4})([^0-9]*)/\1\2-****-****-****\6/g
# Replace IPv4 addresses
# 123.45.67.8 => 123.***.***.8
s/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/\1.***.***.\4/g
@wesley-dean
Copy link
Author

To use:

# via pipe
cat /path/to/file | sanitize.sed

# via redirect
sanitize.sed < /path/to/file

# direct invocation
sanitize.sed /path/to/file

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