Other sed commands
sed
offers a lot of commands that can be used to insert, change, delete, and transform text with ease. Let's see some examples of how to use these commands with sed
.
The delete command
You can use the delete
command d
to delete lines or a range of lines from your stream. The following command will delete the third line from the stream:
$ sed '3d' myfile
The following command will delete the third to the fifth line from the stream:
$ sed '3,5d' myfile
This command will delete from the fourth line to the end of the file:
$ sed '4,$d' myfile
Note that the deletion happens only to the stream, not the actual file. So if you want to delete from the actual file, you can use the -i
option:
$ sed -i '2d' myfile #Permenantly delete the second line from the file
The insert and append commands
The insert, i
, and append, a
, commands work the same way with just a slight difference.
The insert
command inserts the specified text before the specified line or pattern.
The append
command inserts the...