Linux sed and awk Cheat Sheet/Replace first occurrence per line

Substitute one match in each line.

Section: sed Substitution

Replace first occurrence per line

bash
bash
sed 's/foo/bar/' file.txt
Explanation

By default, sed replaces only the first match on each line.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing sed Substitution
Replace all occurrences per line
Substitute every match on each line.
OpenIn sheetbashsame section
Edit a file in place
Modify the original file directly.
OpenIn sheetbashsame section
Edit in place with backup
Create a backup copy while editing.
OpenIn sheetbashsame section
Use an alternate delimiter
Make path replacements easier to read.
OpenIn sheetbashsame section
Delete blank lines
Remove empty lines from output.
OpenIn sheetbash1 tag match
Delete comment lines
Skip lines that begin with `#`.
OpenIn sheetbash1 tag match