Find And Replace Text In File
June 24 2025

Some examples of using the sed command to find and replace text in a file.

# Replace all occurrences
sed -i 's/old_text/new_text/g' filename.txt

# Replace on specific line number
sed '4 s/old_text/new_text/' filename.txt

# Do a dry run
sed -n 's/old_text/new_text/p' filename.txt

# Delete all blank lines
sed '/^$/d' filename.txt