24.07.2018 Views

Bash-Beginners-Guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5. The GNU sed stream editor<br />

At the end of this chapter you will know about the following topics:<br />

♦ What is sed?<br />

♦ Interactive use of sed<br />

♦ Regular expressions and stream editing<br />

♦ Using sed commands in scripts<br />

This is an introduction<br />

These explanations are far from complete and certainly not meant to be used as the<br />

definite user manual for sed. This chapter is only included in order to show some more<br />

interesting topics in the next chapters, and because every power user should have a basic<br />

knowledge of things that can be done with this editor.<br />

For detailed information, refer to the sed info and man pages.<br />

5.1. Introduction<br />

5.1.1. What is sed?<br />

A Stream EDitor is used to perform basic transformations on text read from a file or a pipe. The result is sent<br />

to standard output. The syntax for the sed command has no output file specification, but results can be saved<br />

to a file using output redirection. The editor does not modify the original input.<br />

What distinguishes sed from other editors, such as vi and ed, is its ability to filter text that it gets from a<br />

pipeline feed. You do not need to interact with the editor while it is running; that is why sed is sometimes<br />

called a batch editor. This feature allows use of editing commands in scripts, greatly easing repetitive editing<br />

tasks. When facing replacement of text in a large number of files, sed is a great help.<br />

5.1.2. sed commands<br />

The sed program can perform text pattern substitutions and deletions using regular expressions, like the ones<br />

used with the grep command; see Section 4.2.<br />

The editing commands are similar to the ones used in the vi editor:<br />

Table 5-1. Sed editing commands<br />

Command Result<br />

a\ Append text below current line.<br />

c\ Change text in the current line with new text.<br />

d Delete text.<br />

i\ Insert text above current line.<br />

Chapter 5. The GNU sed stream editor 62

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

Saved successfully!

Ooh no, something went wrong!