Interactive editing

5.2. Interactive editing

5.2.1. Printing lines containing a pattern

This is something you can do with grep, of course, but you can't do a "find and replace" using that command. This is just to get you started.

This is our example text file:


sandy ~> cat -n example
     1  This is the first line of an example text.
     2  It is a text with erors.
     3  Lots of erors.
     4  So much erors, all these erors are making me sick.
     5  This is a line not containing any errors.
     6  This is the last line.

sandy ~>

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


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.

sandy ~>

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:


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.

sandy ~>

5.2.2. 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:


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.

sandy ~>

The d command results in excluding lines from being displayed.

Matching lines starting with a given pattern and ending in a second pattern are showed like this:


sandy ~> sed -n '/^This.*errors.$/p' example
This is a line not containing any errors.

sandy ~>

Note that the last dot needs to be escaped in order to actually match. In our example the expression just matches any character, including the last dot.

5.2.3. 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:


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.

sandy ~>

To print the file starting from a certain line until the end of the file, use a command similar to this:


sandy ~> sed '3,$d' example
This is the first line of an example text.
It is a text with erors.

sandy ~>

This only prints the first two lines of the example file.

The following command prints the first line containing the pattern "a text", up to and including the next line containing the pattern "a line":


sandy ~> 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.

sandy ~>

5.2.4. 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.


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.

sandy ~>

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:


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.

sandy ~>

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


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.

sandy ~>

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

sandy ~>

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.

sandy ~>

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

TipMore examples
 

Plenty of sed examples can be found in the startup scripts for your machine, which are usually in /etc/init.d or /etc/rc.d/init.d. Change into the directory containing the initscripts on your system and issue the following command:

grep sed *


189 visits (1 today, 8 this week, 38 this month, 189 this year)
Uptime: 19:50:32 up 10 days, 1:33, 7 users, load average: 0.00, 0.00, 1.17
3.133.149.244 GET from server z.larryforalaska.com

Friday, December 27, 2024 @ 7:50:32 PM
z.ServerAdmin@lam1.us