Sunday 26 February 2017

sed command examples for practice for Linux beginners

A Stream EDitor is used to perform basic transformations on text read from a file or a pipe

Consider a text file called example which has data like below
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.

Printing lines containing a pattern
We want sed to find all the lines containing our search pattern, in this case "erors". We use the p to obtain the result

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.

As you notice, sed prints the entire file, but the lines containing the search string are printed twice. This is not what we want. In order to only print those lines matching our pattern, use the -n option:

sed -n '/erors/p' example
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.


Deleting lines of input containing a pattern
We use the same example text file. Now we only want to see the lines not containing the search string:

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.

Ranges of lines
This time we want to take out the lines containing the errors. In the example these are lines 2 to 4. Specify this range to address, together with the d command:

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.
The following command prints the first line containing the pattern "a text", up to and including the next line containing the pattern "a line":

sed -n '/a text/,/This/p' example
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.

Find and replace with sed
In the example file, we will now search and replace the errors instead of only (de)selecting the lines containing the search string.

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.

As you can see, this is not exactly the desired effect: in line 4, only the first occurrence of the search string has been replaced, and there is still an 'eror' left. 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:

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.

To insert a string at the beginning of each line of a file, for instance for quoting:

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:

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:

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.

Just Keep in mind that by default sed prints its results to the standard output, most likely your terminal window. If you want to save the output to a file, redirect it:

sed option 'some/expression' file_to_process > sed_output_in_a_file

if you want to save the formatted text you can use like below

sed -i 's/erors/errors/g' example





No comments:

Post a Comment