sed Replace String in File

Use sed to replace text in one file or many files, with examples for Linux and macOS.

Replace First Match Per Line

sed 's/old/new/' app.conf

Replace All Matches Per Line

sed 's/old/new/g' app.conf

Edit File In Place

# GNU sed
sed -i 's/old/new/g' app.conf

# macOS/BSD sed
sed -i '' 's/old/new/g' app.conf

Common Mistake

macOS sed requires an argument after -i. Use -i '' for no backup, or -i .bak to create a backup file.

Related

Knowledge is power.