sed
CLIStream editor for filtering and transforming text.
Stream editor for in-place text substitution
Verified commands
Executed by our harness on the platforms shown; the assertion checked the actual result, not just the exit code.
Replace text in a file in place (macOS/BSD sed)
macOS only
sed -i '' 's/old/new/g' file.txtCommon failures (1)
- on Linux: sed: can't read : No such file or directoryGNU sed takes no argument after -i: use sed -i 's/old/new/g' file.txt
Replace text in a file in place (Linux/GNU sed)
Linux only
sed -i 's/old/new/g' file.txtCommon failures (1)
- on macOS: sed: 1: "file.txt": invalid command codeBSD sed needs an explicit backup suffix: sed -i '' 's/old/new/g' file.txt
Replace text in a file in place (portable, any OS)
perl -pi -e 's/old/new/g' file.txtReplace text across all files in a directory (portable)
grep -rl 'old' . | xargs perl -pi -e 's/old/new/g'Common failures (1)
- binary or .git files modifiedrestrict with --include: grep -rl --include='*.ts' 'old' src | xargs perl -pi -e 's/old/new/g'
Fallback
rg -l 'old' . | xargs perl -pi -e 's/old/new/g'More recipes
Curated from documentation and usage. Percentages are estimates, not measurements.
Delete lines matching pattern
sed '/pattern/d' file.txtPrint specific line number
sed -n '5p' file.txtInstall
Homebrew
brew install gnu-sedAgent notes
- When to use
- Simple find-and-replace in files or streams
- Quick examples
sed 's/foo/bar/g' file.txtsed -i 's/old/new/g' *.txt
Use from an agent
curl -s https://clihub.com/api/tools/sed | jq '.agentHints.provenRecipes[] | {task, command, verified}'Or call get_tool with slug sed on the MCP server at https://clihub.com/mcp.