macOS: sed: 1: "file.txt": invalid command code f / extra characters at the end of p command
macOS ships BSD sed, where -i requires a backup-suffix argument, unlike GNU sed on Linux.
sed: 1: invalid command codesed: 1: "file": invalid command code fsed: extra characters at the end of p commandsed: -i may not be used with stdinsed: RE error: illegal byte sequence Meaning
Use sed -i '' 's/a/b/' file on macOS (empty string = no backup) or sed -i.bak ... for both. For scripts that must work on Linux and macOS use perl -pi -e, or install GNU sed (brew install gnu-sed, command gsed).
Common causes
- GNU-style
sed -i 's/a/b/' fileon macOS - Missing empty suffix argument
\n/\tescapes not supported in BSD replacement- Script written for Linux
-roption (use -E)
⚡ Quick fix
sed -i '' 's/a/b/' fileon macOS- Portable:
sed -i.bak 's/a/b/' file && rm file.bak brew install gnu-sedand usegsed
Detailed fix by platform
macOS
sed -i.bak 's/foo/bar/g' file && rm file.bakworks on both systems
How to diagnose
- OS — macOS (BSD)?
- Flag — -i suffix?
- Portable — perl or gsed?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- command not found bash: command not found
- Homebrew Cask: It seems there is already an App at Homebrew: Error: It seems there is already an App at '/Applications/Chrome.app'
- Homebrew: Failed to update / fatal: unable to access Homebrew: Error: Failure while executing; `git fetch --force origin` exited with 128 / fatal: not in a git directory
Most viewed in macOS
- is damaged and can’t be opened macOS: “App” is damaged and can’t be opened. You should move it to the Trash.
- Operation not permitted (macOS privacy) macOS: Operation not permitted — Terminal/app lacks Full Disk Access (TCC privacy protection)
- zsh: command not found: brew macOS: zsh: command not found: brew (Homebrew not on PATH on Apple Silicon)
Other ways to find it
Where this error sits
Origin: Platform & cloud. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026