What is sed?
sed (stream editor) is a powerful command-line tool for parsing and transforming text. It processes text line by line, making it perfect for automated edits in scripts.
Basic Substitution
# Replace first occurrence per line
sed "s/old/new/" file.txt
# Replace all occurrences per line
sed "s/old/new/g" file.txt
# Case-insensitive
sed "s/old/new/gi" file.txt
# Edit file in place
sed -i "s/old/new/g" file.txtCommon Use Cases
# Change config values
sed -i "s/listen_port=80/listen_port=8080/" config.ini
# Delete lines matching pattern
sed -i "/^#/d" config.conf # Remove comments
sed -i "/^$/d" file.txt # Remove empty lines
# Insert line after match
sed -i "/\[server\]/a listen_port=8080" config.ini
# Print only matching lines
sed -n "/error/p" logfile.logAddress Ranges
# Apply to line range
sed "5,10s/foo/bar/g" file.txt
# Apply from pattern to end
sed "/START/,$ s/foo/bar/g" file.txt