Linux sed and awk Cheat Sheet/Replace all occurrences per line

Substitute every match on each line.

Section: sed Substitution

Replace all occurrences per line

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

The `g` flag applies the replacement globally across the 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 first occurrence per line
Substitute one match in 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