Basic Format
sed 'options'... 'commands' fileKey Expressions
| p | Print the line. |
|
| d | Delete the line. |
|
| s/regexp/replacement/ | Substitute 'regexp' with 'replacement'. |
|
| i\ | Insert text before the line. |
|
| a\ | Append text after the line. |
|
| c\ | Replace the selected line(s) with text. |
|
| n | Read the next input line. |
|
| q | Quit sed. |
Examples
| sed 's/foo/bar/' file | Replace first occurrence of 'foo' with 'bar' in each line. |
|
| sed 's/foo/bar/g' file | Replace all occurrences of 'foo' with 'bar' in each line. |
|
| sed '/foo/d' file | Delete lines containing 'foo'. |
|
| sed '/foo/!d' file | Delete lines not containing 'foo'. |
|
| sed '3d' file | Delete the 3rd line of the file. |
|
| sed '3,5d' file | Delete lines 3 through 5. |
|
| sed '3,5p' file | Print lines 3 through 5. |
|
| sed '3,$d' file | Delete lines from 3 to end of file. |
|
| sed '10q' file | Print the first 10 lines (emulates 'head'). |
|
| sed -n '/foo/p' file | Print only lines matching 'foo'. |
|
| sed 's/foo/bar/2' file | Replace the second occurrence of 'foo' with 'bar'. |
|
| sed 's/foo/bar/gi' file | Replace 'foo' with 'bar' case-insensitively. |
|
| sed '1i\Hello' file | Insert 'Hello' before the first line. |
|
| sed '$a\Goodbye' file | Append 'Goodbye' after the last line. |