I have a text file that contains the following lines.

Maynard G. Krebs , ss #111−22−3333, the goofy but lovable beatnik in The Many
Loves of Dobie Gillis. None of the show's episodes ever specified Gilligan's

How do i use sed to replace the number #111-22-3333 with xxx-xx-xxxx?

This is what i have in my sed script: s/([0-9]{3}-[0-9]{2}-[0-9]{4})/xxx-xx-xxxx/g
I run it with sed -f redact.sed redact.txt

1 Answer 1

  • Drop the parenthesis, or Sed will try to match a parenthesis in your file.

  • {n} is part of extended regular expressions. Give Sed the -E flag to enable them (sed -Ef redact.sed redact.txt), or escape each brace.

  • Notice your file contains some sort of en-dash (0xE28892): It is not the good old 0x2D ASCII -, so that you would need to put it in the Sed expression too.

    sed 's/[0-9]\{3\}−[0-9]\{2\}−[0-9]\{4\}/xxx-xx-xxxx/g'
    sed -E 's/[0-9]{3}−[0-9]{2}−[0-9]{4}/xxx-xx-xxxx/g'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .