Stream Editor Managing Patterns - Sed (Stream Editor)

How to Manage Patterns in Stream Editor?

The use of pattern and hold buffer have been specified in the earlier chapters.The n command is dicussed in this to print the pattern space and are used in conjunction with other commands.

The syntax of n command is shown below:

An example is illustrated as follows:

The above code is executed and the following result is obtained:

The n command prints the contents of the pattern buffer, clears the pattern buffer, fetches the next line into the pattern buffer, and applies commands on it.

Consider three SED commands before n and two SED commands after n as follows:

  • SED applies the first three commands on the pattern buffer, clears the pattern buffer, fetches the next line into the pattern buffer, and thereafter applies the fourth and fifth commands on it.
  • Without having a clear understanding of this concept do not proceed further as this is a very important concept.
  • The hold buffer holds data, but SED commands cannot be applied directly on the hold buffer. So, there is a need to bring the hold buffer data into the pattern buffer.
  • The x command is provided by SED to exchange the contents of pattern and hold buffers. The x command is illustrated by the following commands.

The books.txt file need to be modified slightly. The file contains book titles followed by their author names. After modification, the file should look like this:

The following result is obtained on executing the above code:

Exchange the contents of the two buffers. For instance, the following example prints only the names of authors.

The following result is obtained on executing the above code:

The working of this command is illustrated as follows::

  • Initially, SED reads the first line, i.e., A Storm of Swords into the pattern buffer.
  • x command moves this line to the hold buffer.
  • n fetches the next line, i.e., George R. R. Martin into the pattern buffer.
  • The control passes to the command followed by n which prints the contents of the pattern buffer.
  • The process repeats until the file is exhausted.

Now, exchange the contents of the buffers before printing.It prints the titles of books.

The above code is executed and the following result is obtained:

The h command deals with the hold buffer and copies data from the pattern buffer to the hold buffer. Existing data from the hold buffer gets overwritten.

Note: The h command does not move data, it only copies data. So, the copied data remains as it is in the pattern buffer. The below syntax illustrates the h command.

The following command prints only the titles of the author Paulo Coelho.

The above code is executed and the following result is obtained:

The contents of books.txt follow a specific format.

  • The first line is the book title followed by the author of the book. The "!" is used to reverse the condition, i.e., line is copied to the hold buffer only when a pattern match do not succeed. Multiple SED commands can be grouped using curly braces {}.
  • In the first pass of the command, SED reads the first line, i.e., A Storm of Swords into the pattern buffer and checks whether it contains the pattern Paulo or not.

As the pattern match does not succeed, it copies this line to the hold buffer. Now both the pattern buffer and the hold buffer contain the same line i.e., A Storm of Swords.

  • In the second step, it checks whether the line contains the pattern Paulo or not. There is nothing to do if the pattern doesn’t match.
  • In second pass, the next line George R. R. Martin is read into the pattern buffer and applies the same steps and repeats the same for the next three lines.
  • At the end of the fifth pass, both the buffers contain The Alchemist.
  • At the start of the sixth pass, the line Paulo Coelho is read and as the pattern matches, it does not copy this line into the hold buffer. Hence, the pattern buffer contains Paulo Coelho, and the hold buffer contains The Alchemist.
  • Thereafter, it checks whether the pattern buffer contains the pattern Paulo. It exchanges the contents of the pattern buffer with the hold buffer, when the pattern matches.
  • The pattern buffer contains The Alchemist and the hold buffer contains Paulo Coelho. Finally, the contents of the pattern buffer are printed and thee same steps are applied to the pattern The Pilgrimage.
  • The h command destroys the previous contents of the hold buffer. Sometimes the contents need to be preserved as they are not acceptable always. SED provides the H command which appends the contents to the hold buffer by adding a new line at the end.
  • The only difference between h and H command is, the former overwrites data from the hold buffer, while the later appends data to the hold buffer.

The syntax of h command is similar to this.

Another example is illustrated as follows. This time, instead of printing only book titles, print the names of their authors too. The following example prints the book titles followed by their author names.

The following result is obtained on executing the above code:

To copy/append the contents of pattern buffer to hold buffer is illustrated. The reverse functions can also be performed. For this purpose, SED provides the g command which copies data from the hold buffer to the pattern buffer. While copying, existing data from the pattern space gets overwritten.

The syntax of the g command is as follows.

The example of printing book titles and their authors.
Now, the name of the author is first printed and on the next line, the corresponding book title. The below command prints the name of the author Paulo Coelho, followed by its book title.

The following result is obtained on executing the above code:

The first command is kept the same.

  • At the end of fifth pass, both the buffers contain The Alchemist.
  • At the start of the sixth pass, the line Paulo Coelho is read and as the pattern matches, it does not copy this line into the hold buffer. Hence, the pattern space contains Paulo Coelho and the hold space contains The Alchemist.
  • Check whether the pattern space contains the pattern Paulo. As the pattern match succeeds, it first prints the contents of the pattern space, i.e., Paulo Coelho, then it copies the hold buffer to the pattern buffer. Hence, both the pattern and hold buffers contain The Alchemist. Finally, it prints the contents of the pattern buffer.
  • Similarly, the contents of the hold buffer can be appended to the pattern buffer. SED provides the G command which appends the contents to the pattern buffer by adding a new line at the end.

Consider previous example which prints the name of author Paulo Coelho followed by its book title. To achieve the same result, execute the following SED command.

The following result is obtained on executing the above code:

The above example can be modified to display the book titles followed by their authors by exchanging the buffer contents before the G command.

The following result is obtained on executing the above code:

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Sed (Stream Editor) Topics