sed Linux Command
Stream editor for filtering and transforming text (find/replace, insert, delete).
What does sed do?
Stream editor for filtering and transforming text (find/replace, insert, delete).
Syntax
sed [options] 'command' filePractical example
sed 's/old/new/g' file.txtQuestions about sed
How do I replace text with sed?
Use `sed 's/old/new/g' file` to replace all occurrences of 'old' with 'new'. The `g` flag replaces all matches per line.
How do I replace text in place?
Use `sed -i 's/old/new/g' file` to modify the file directly. Without `-i`, sed only prints the output.
How do I delete a specific line with sed?
Use `sed '5d' file` to delete line 5, or `sed '/pattern/d' file` to delete lines matching a pattern.
How do I insert text before a line?
Use `sed '3i\new line' file` to insert 'new line' before line 3. The `i` command inserts text.
How do I append text after a line?
Use `sed '3a\new line' file` to append 'new line' after line 3. The `a` command appends text.
How do I delete the last line with sed?
Use `sed '$d' file` to delete the last line. The `$` address refers to the last line of the file.
How do I view just a specific line with sed?
Use `sed -n '5p' file` to print only line 5. The `-n` suppresses default output, and `p` prints the specified line.
How do I use sed with regex?
Use `sed 's/[0-9]*/ /g' file` to work with regular expressions. Sed supports extended regex with `-E` flag.
Related Search Commands
awkPowerful text processing language for pattern scanning and data extraction.
sortSorts lines of text files alphabetically or numerically.
typeDisplays how a command name would be interpreted (alias, builtin, external, etc.).
uniqReports or filters out repeated lines in a sorted file.
whereisLocates the binary, source, and manual page files for a command.
cutExtracts specific columns/fields from each line of a file.