Replace the first match in each line

Replace the first "foo" with "bar" in each line

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

tool
sed15 commands
tags
sedlinux

More sed commands

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

    Replace every match in each line with the /g flag

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

    Change the file in place without a backup; on macOS use -i ''

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

    Edit in place and keep the original as file.txt.bak

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

    Delete all comment lines that start with #

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

    Remove empty lines and lines with only spaces

all 15 sed commands