sed Commands
Stream editor for filtering and transforming text.All sed pages
- sed -i 's/old/new/g' file: find and replace text in a filesed -i 's/old/new/g' file replaces text in place; without g only the first match per line. Backups with -i.bak, other delimiters, and the macOS -i '' form.
- sed -n '10,20p' file: print specific lines or a line rangesed -n '5p' prints line 5 and sed -n '10,20p' prints lines 10 to 20. -n turns off auto-printing; add q to stop early on big files. Also last line and steps.
- sed '/pattern/d': delete lines matching a patternsed '/pattern/d' file deletes every line matching a regex; '/pattern/!d' keeps only matches, and -i edits in place. Ranges, empty lines and BSD sed notes.
- sed insert a line after or before a matching line (a and i commands)sed '/pattern/a text' adds a line after each match and i adds one before; $a appends at the end. GNU one-liners, the portable form for macOS, first match only.