Using a Stream Editor

In scripting you can use some fixed Bash functionality, such as if...then...else, for, read, and others that you have read about in this chapter. To make a script really powerful, you can use external utilities as well. One of these is the stream editor sed. In this section, I'll introduce you to some of the sed basics.

The stream editor sed can be compared to grep. Where grep is merely used to find patterns in files, sed does something to these patterns as well. To accomplish this, a sed command consists of different parts. In the first part, you indicate what exactly you want the command to do. Then you specify for what it has to search. Next, you can specify a pattern to indicate the replacement text, and finally you can specify how a replacement has to take place. You can see an example of this in the following line:

sed "s/english/french/g" languages.txt

In this example, the action that has to be performed is a substitution (s). The text that has to be located is english, and its replacement text is french. Finally, the letter g indicates that the command has to be executed as long as matches are found. Also notice that the command sed has to execute is always between quotes, which prevents the shell from interpreting the text string.

The last part you have to be aware of when working with sed is that the command will never modify the original file. It will write the modifications it makes to the original text to STDOUT. If you want these modifications to be saved somewhere, you need to write them to a new file. The most common way to do that is by redirecting to a temporary file. If so required, you can later use the temporary file to overwrite the original file. You would modify the previous example to the following to accomplish this:

sed "s/english/french/g" languages.txt > languages2.txt Next, you can copy the new output file over the old file.

Another useful task you can accomplish with sed is removing text from a file. In that case, you just add an empty replacement text. An example of this is the following command:

sed "s/something//g" list.txt

Of course, you have to make sure the result is written to some temporary file. Also useful is the option to remove lines that match a certain pattern from a file. For example, the following command will remove user sander from the /etc/passwd file:

sed "/sander/d" /etc/passwd

Notice that in this example no substitution is used anymore, but the d (delete) command removes the line. You can even make it somewhat more complicated by removing an empty line. In that case, you need to work with a regular expression. The next example shows how:

The special construction that is in use here is a regular expression that searches for the beginning of the line, indicated by $, which is followed immediately by the end of the line, which is indicated by $. Since nothing appears between the two of them, this construction helps you find empty lines.

Was this article helpful?

0 0

Post a comment