24.07.2018 Views

Bash-Beginners-Guide

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

p Print text.<br />

r Read a file.<br />

s Search and replace text.<br />

w Write to a file.<br />

Apart from editing commands, you can give options to sed. An overview is in the table below:<br />

Table 5-2. Sed options<br />

Option<br />

-e SCRIPT<br />

-f<br />

Effect<br />

-n Silent mode.<br />

Add the commands in SCRIPT to the set of commands to be run while processing the<br />

input.<br />

Add the commands contained in the file SCRIPT-FILE to the set of commands to be<br />

run while processing the input.<br />

-V Print version information and exit.<br />

The sed info pages contain more information; we only list the most frequently used commands and options<br />

here.<br />

5.2. Interactive editing<br />

5.2.1. Printing lines containing a pattern<br />

This is something you can do with grep, of course, but you can't do a "find and replace" using that command.<br />

This is just to get you started.<br />

This is our example text file:<br />

sandy ~> cat -n example<br />

1 This is the first line of an example text.<br />

2 It is a text with erors.<br />

3 Lots of erors.<br />

4 So much erors, all these erors are making me sick.<br />

5 This is a line not containing any errors.<br />

6 This is the last line.<br />

sandy ~><br />

We want sed to find all the lines containing our search pattern, in this case "erors". We use the p to obtain the<br />

result:<br />

sandy ~> sed '/erors/p' example<br />

This is the first line of an example text.<br />

It is a text with erors.<br />

It is a text with erors.<br />

Lots of erors.<br />

Lots of erors.<br />

So much erors, all these erors are making me sick.<br />

So much erors, all these erors are making me sick.<br />

This is a line not containing any errors.<br />

This is the last line.<br />

Chapter 5. The GNU sed stream editor 63

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!