Stream Editor in the UNIX Shell

(Page 1 of 5 )

Making a change sometimes takes a great effort coupled with a great sacrifice. At other times, though, it simply calls for a little adjustment of what we already have. The stream editor does just this. Since inside the UNIX shell, the universal language spoken is the text stream, with the help of this tool you can make some easy and fast modifications to the shell. If you are interested in what you can change and how you can change it with the stream editor, you will have to read this article.

This article is part of my series related to shell programming in UNIX. Therefore, I will build on the fact that you can already write a script and run it inside a terminal. In addition, you should also know how to handle the control mechanism of the UNIX shell. All of these things are explained in my previous articles, so if by chance you missed them just search them up and embrace their knowledge.

The structure of this article is as follows. First, I will present the syntax of the command. This will be followed by a general description of how this command works. Finally, I will present a few commands, separated into often-used and rarely-used groups, and those related to the hold puffer.

Of course, I will include examples for all this to help you understand what I'm trying to explain. The ultimate goal is that you understand how it works. The sed is the acronym for the stream editor. You can already figure out that with this tool, you can edit streams of data. By this, I refer especially stream texts.

The tool will read line after line of a text and make changes to it on request, or simply let it all through. The syntax of the tool is the following:

sed [options] 'script' [input files]

Where the options can be:

Option

What it means

-e

The script we define as an argument

-f

The script is inside a file

-n

Do not print on the output after processing the stream

-r

Work with the extended regular expressions

-i

Edit the file itself

-s

If we give multiple inputs, work with the files individually

-g

At all the replacements, use the global option

From the upper table you can already see that the "script" part is not obligatory as an argument. If you want it, you can write it inside a file. The stream editor has its own commands to process the text. This will be the subject of the third section of the article. We can enter this into a separate file, and this will be the script. If you choose to use the file, you will need to enter every command on a new line and start the script with the following line:

#!/bin/sed -f

This will allow you to later call the sed without appending the -f option each time, as follows:

sed script_file alfa

This will execute on every line of the alfa file the commands found inside the script_file. However, this is a simplified approach. To find out more about how the stream editor works, read the next page.