Find-and-replace and line surgery from the command line. Tap any command to copy it.
sed 's/old/new/' file.txtReplace the first match on each linesed 's/old/new/g' file.txtReplace all matches on each linesed 's/old/new/gi' file.txtReplace all, case-insensitivesed -i 's/old/new/g' file.txtEdit the file in placesed -i.bak 's/old/new/g' file.txtEdit in place, keep a .bak backupsed 's|/old/path|/new/path|g' fileUse | as the delimiter for pathssed -n '5p' file.txtPrint only line 5sed -n '5,10p' file.txtPrint lines 5 through 10sed '5d' file.txtDelete line 5sed '/pattern/d' file.txtDelete every line matching a patternsed '$d' file.txtDelete the last linesed -n '/start/,/end/p' file.txtPrint a range between two patternssed 's/^/ /' file.txtIndent every line by four spacessed 's/[[:space:]]*$//' file.txtStrip trailing whitespacesed '/^$/d' file.txtRemove blank linessed '3i\new line' file.txtInsert a line before line 3sed '3a\new line' file.txtAppend a line after line 3sed -e 's/a/b/' -e 's/c/d/' fileApply several edits in one passsed is a "stream editor" — it reads text line by line and applies edits. The one command you'll use ninety percent of the time is substitute: s/old/new/ replaces text, and adding the g flag makes it replace every occurrence on a line instead of just the first. So sed 's/colour/color/g' fixes every instance on every line. The delimiter doesn't have to be a slash — when you're editing file paths, s|/old|/new| avoids a mess of escaped slashes.
By default sed prints the result and leaves the original file alone, which is great for previewing. To actually change the file, add -i (in place). The safe habit is -i.bak, which edits the file but first saves the original as file.bak — cheap insurance against a regex that matched more than you intended. A subtle gotcha: macOS (BSD) sed needs an explicit backup suffix, even an empty one, written as sed -i '' '...', whereas GNU sed on Linux is happy with a bare -i.
Beyond search-and-replace, sed can target lines by number or pattern. sed -n '5,10p' prints just lines 5 to 10 (the -n suppresses the default output so only the printed lines show), sed '5d' deletes line 5, and sed '/pattern/d' deletes every line matching a pattern — a quick way to strip blank lines or comments. For column-oriented data and arithmetic rather than line edits, reach for awk; to find the lines first, combine sed with grep.