Substitute all matches (global)

Replace every occurrence per line with the /g flag

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

tool
sed15 commands
tags
sedlinux

More sed commands

  • sed -i 's/old/new/g' config.cfg

    Modify file in-place (no backup); on macOS add empty string: -i ''

  • sed -i.bak 's/old/new/g' file.txt

    Edit in-place and save original as file.txt.bak

  • sed '/^#/d' config.txt

    Delete all comment lines starting with #

  • sed '/^[[:space:]]*$/d' file.txt

    Remove all empty or whitespace-only lines

  • sed -n '10,20p' file.txt

    Print lines 10 through 20 only (suppress default output with -n)

all 15 sed commands