sed Regex Tester
sed is one of those tools where the regex is only half the problem. The rest is shell quoting and replacement syntax.
I still test the regular expression separately first. Then I put it into sed.
Use extended regexes on macOS
On macOS, I usually use sed -E:
sed -E 's/old/new/g' file.txt
That enables extended regular expressions. It keeps patterns easier to read because I do not need to escape common grouping and alternation syntax as much.
Example input:
AUTH-1284 fixed
REGEX-42 needs review
BILLING-9001 shipped
Pattern:
([A-Z]+)-([0-9]+)
Replacement:
\1 ticket \2
sed command:
sed -E 's/([A-Z]+)-([0-9]+)/\1 ticket \2/g' tickets.txt
Result:
AUTH ticket 1284 fixed
REGEX ticket 42 needs review
BILLING ticket 9001 shipped
Test the match and the replacement separately
When I am working on a sed command, I split the problem:
- Test the regex against sample text.
- Check the capture groups.
- Then write the replacement string.
This avoids the usual “why did sed replace the wrong thing?” loop.
Regex for macOS helps with the first two steps. It shows the matches and capture groups while I edit the pattern.
One macOS sed detail to watch: I do not use \b there as a word-boundary shortcut. If I need strict boundaries in sed, I test that exact sed expression instead of assuming it behaves like Swift, JavaScript, or Ruby.
Watch out for shell quoting
The pattern that works in a regex tester may need quoting changes in Terminal.
I normally wrap the sed expression in single quotes:
sed -E 's/foo/bar/g' file.txt
If the replacement needs a literal single quote, I stop and make the command easier to read. Sometimes that means using a small Ruby, Python, or Perl script instead of turning the shell command into punctuation soup.
sed is not a parser
sed is great for line-oriented text changes:
- renaming simple tokens
- changing config values
- cleaning generated output
- quick one-off migrations
It is not what I use for structured formats when a parser is available. For JSON, HTML, XML, and non-trivial CSV, I use a proper parser.
My sed regex checklist
Before I run a destructive command, I check:
- Does the regex match only the lines I want?
- Are capture groups in the right order?
- Does the replacement preserve the parts I need?
- Have I tested on a copy or printed output first?
- Do I need
-E? - Is
sedstill the right tool?
Related: Regex tester for macOS and Swift Regex Builder.