sed command examples Flashcards
Sed editing commands
Sed options
find all the lines containing pattern “erors”
sandy ~> sed ‘/erors/p’ example
This is the first line of an example text.
It is a text with erors. It is a text with erors.
Lots of erors. Lots of erors.
So much erors, all these erors are making me sick.
So much erors, all these erors are making me sick.
This is a line not containing any errors.
This is the last line.
only print those lines matching our pattern
sandy ~> sed -n ‘/erors/p’ example
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.
see the lines not containing the search string
sandy ~> sed ‘/erors/d’ example
This is the first line of an example text.
This is a line not containing any errors.
This is the last line.
Matching lines starting with one pattern and ending in a second pattern
sandy ~> sed -n ‘/^This.*errors.$/p’ example
This is a line not containing any errors.
take out the lines containing the errors
sandy ~> sed ‘2,4d’ example
This is the first line of an example text.
This is a line not containing any errors.
This is the last line.
print the file starting from a certain line until the end of the file
sandy ~> sed ‘3,$d’ example
This is the first line of an example text.
It is a text with erors.
search and replace the errors instead of only (de)selecting the lines containing the search string.
sandy ~> sed ‘s/erors/errors/’ example
This is the first line of an example text.
It is a text with errors. Lots of errors.
So much errors, all these erors are making me sick.
This is a line not containing any errors.
This is the last line.
Use the g command to indicate to sed that it should examine the entire line instead of stopping at the first occurrence of your string
sandy ~> sed ‘s/erors/errors/g’ example
This is the first line of an example text.
It is a text with errors.
Lots of errors.
So much errors, all these errors are making me sick.
This is a line not containing any errors.
This is the last line.
insert a string at the beginning of each line of a file
sandy ~> sed ‘s/^/> /’ example
> This is the first line of an example text.
> It is a text with erors.
> Lots of erors.
> So much erors, all these erors are making me sick.
> This is a line not containing any errors.
> This is the last line.
Insert some string at the end of each line
sandy ~> sed ‘s/$/EOL/’ example
This is the first line of an example text.EOL
It is a text with erors.EOL
Lots of erors.EOL
So much erors, all these erors are making me sick.EOL
This is a line not containing any errors.EOL
This is the last line.EOL
Multiple find and replace commands are separated with individual -e options
sandy ~> sed -e ‘s/erors/errors/g’ -e ‘s/last/final/g’ example
This is the first line of an example text.
It is a text with errors.
Lots of errors.
So much errors, all these errors are making me sick.
This is a line not containing any errors.
This is the final line.