Monday, April 3, 2017

Stream Editor - Quick Guide

Stream Editor - Overview

The acronym SED stands for Stream EDitor. It is a simple yet powerful utility that parses the text and transforms it seamlessly. SED was developed during 1973–74 by Lee E. McMahon of Bell Labs. Today, it runs on all major operating systems.

McMahon wrote a general-purpose line-oriented editor, which eventually became SED. SED borrowed syntax and many useful features from ed editor. Since its beginning, it has support for regular expressions. SED accepts inputs from files as well as pipes. Additionally, it can also accept inputs from standard input streams.
SED is written and maintained by the Free Software Foundation (FSF) and it is distributed by GNU/Linux. Hence it is often referred to as GNU SED. To a novice user, the syntax of SED may look cryptic. However, once you get familiar with its syntax, you can solve many complex tasks with a few lines of SED script. This is the beauty of SED.

Typical Uses of SED

SED can be used in many different ways, such as:
  • Text substitution,
  • Selective printing of text files,
  • In-a-place editing of text files,
  • Non-interactive editing of text files, and many more.

Stream Editor - Environment

This chapter describes how to set up the SED environment on your GNU/Linux system.

Installation Using Package Manager

Generally, SED is available by default on most GNU/Linux distributions. Use which command to identify whether it is present on your system or not. If not, then install SED on Debian based GNU/Linux using apt package manager as follows:
[jerry]$ sudo apt-get install sed 
After installation, ensure that SED is accessible via command line.
[jerry]$ sed --versio
On executing the above code, you get the following result:
sed (GNU sed) 4.2.2 
Copyright (C) 2012 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later . 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law.  
Written by Jay Fenlason, Tom Lord, Ken Pizzini, 
and Paolo Bonzini. 
GNU sed home page: . 
General help using GNU software: . 
E-mail bug reports to: . 
Be sure to include the word "sed" somewhere in the "Subject:" field.
Similarly, to install SED on RPM based GNU/Linux, use yum package manager as follows:
[root]# yum -y install sed
After installation, ensure that SED is accessible via command line.
[root]# sed --version
On executing the above code, you get the following result:
GNU sed version 4.2.1 
Copyright (C) 2009 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions.  There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, 
to the extent permitted by law.  
GNU sed home page: . 
General help using GNU software: . 
E-mail bug reports to: . 
Be sure to include the word "sed" somewhere in the "Subject:" field.

Installation from Source Code

As GNU SED is a part of the GNU project, its source code is available for free download. We have already seen how to install SED using package manager. Let us now understand how to install SED from its source code.
The following installation is applicable to any GNU/Linux software, and for most other freely-available programs as well. Here are the installation steps:
  • Download the source code from an authentic place. The command-line utility wget serves this purpose.
  • [jerry]$ wget ftp://ftp.gnu.org/gnu/sed/sed-4.2.2.tar.bz2
    
  • Decompress and extract the downloaded source code.
  • [jerry]$ tar xvf sed-4.2.2.tar.bz2 
    
  • Change into the directory and run configure.
  • [jerry]$ ./configure 
    
  • Upon successful completion, the configure generates Makefile. To compile the source code, issue a make command.
  • [jerry]$ make
    
  • You can run the test suite to ensure the build is clean. This is an optional step.
  • [jerry]$ make check 
    
  • Finally, install the SED utility. Make sure you have superuser privileges.
  • [jerry]$ sudo make install 
    
That is it! You have successfully compiled and installed SED. Verify it by executing the sed command as follows:
[jerry]$ sed --version
On executing the above code, you get the following result:
sed (GNU sed) 4.2.2 
Copyright (C) 2012 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later . 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law.  
Written by Jay Fenlason, Tom Lord, Ken Pizzini, 
and Paolo Bonzini. 
GNU sed home page: . 
General help using GNU software: . 
E-mail bug reports to: . 
Be sure to include the word "sed" somewhere in the "Subject:" field.

Stream Editor - Workflow

In this chapter, we will explore how SED exactly works. To become an expert SED user, one needs to know its internals. SED follows a simple workflow: Read, Execute, and Display. The following diagram depicts the workflow.
Stream Editor Workflow
  • Read: SED reads a line from the input stream (file, pipe, or stdin) and stores it in its internal buffer called pattern buffer.
  • Execute: All SED commands are applied sequentially on the pattern buffer. By default, SED commands are applied on all lines (globally) unless line addressing is specified.
  • Display: Send the (modified) contents to the output stream. After sending the data, the pattern buffer will be empty.
  • The above process repeats until the file is exhausted.

Points to Note

  • Pattern buffer is a private, in-memory, volatile storage area used by the SED.
  • By default, all SED commands are applied on the pattern buffer, hence the input file remains unchanged. GNU SED provides a way to modify the input file in-a-place. We will explore about it in later sections.
  • There is another memory area called hold buffer which is also private, in- memory, volatile storage area. Data can be stored in a hold buffer for later retrieval. At the end of each cycle, SED removes the contents of the pattern buffer but the contents of the hold buffer remains persistent between SED cycles. However SED commands cannot be directly executed on hold buffer, hence SED allows data movement between the hold buffer and the pattern buffer.
  • Initially both pattern and hold buffers are empty.
  • If no input files are provided, then SED accepts input from the standard input stream (stdin).
  • If address range is not provided by default, then SED operates on each line.

Examples

Let us create a text file quote.txt to contain a quote of the famous author Paulo Coelho.
[jerry]$ vi quote.txt 
There is only one thing that makes a dream impossible to achieve: the fear of failure. 
 - Paulo Coelho, The Alchemist
To understand the workflow of SED, let us display the contents of the file quote.txt using SED. This example simulates the cat command.
[jerry]$ sed '' quote.txt
When the above code is executed, it will produce the following result.
There is only one thing that makes a dream impossible to achieve: the fear of failure. 
In the above example, quote.txt is the input file name and before that there is a pair of single quote that implies the SED command. Let us demystify this operation.
First SED reads a line from the input file quote.txt and stores it in its pattern buffer. Then it applies SED commands on the pattern buffer. In our case, no SED commands are there, hence no operation is performed on the pattern buffer. Finally it deletes and prints the contents of the pattern buffer on the standard output. Isn't it simple?
In the following example, SED accepts input from the standard input stream.
[jerry]$ sed '' 
When the above code is executed, it will produce the following result.
There is only one thing that makes a dream impossible to achieve: the fear of failure. 
There is only one thing that makes a dream impossible to achieve: the fear of failure.
Here, the first line is entered through keyboard and the second is the output generated by SED. To exit from the SED session, press ctrl-D (^D).

Stream Editor - Basic Syntax

This chapter introduces the basic commands that SED supports and their command-line syntax. SED can be invoked in the following two forms:
sed [-n] [-e] 'command(s)' files 
sed [-n] -f scriptfile files
The first form allows to specify the commands in-line and they are enclosed within single quotes. The later allows to specify a script file that contains SED commands. However, we can use both forms together multiple times. SED provides various command-line options to control its behavior.
Let us see how we can specify multiple SED commands. SED provides the delete command to delete certain lines. Let us delete the 1st, 2nd, and 5th lines. For the time being, ignore all the details of the delete command. We will discuss more about the delete command later.
First, display the file contents using the cat command.
[jerry]$ cat books.txt 
On executing the above code, you get the following result:
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864
Now instruct SED to remove only certain lines. Here, to delete three lines, we have specified three separate commands with -e option.
[jerry]$ sed -e '1d' -e '2d' -e '5d' books.txt 
On executing the above code, you get the following result:
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
6) A Game of Thrones, George R. R. Martin, 864 
Additionally, we can write multiple SED commands in a text file and provide the text file as an argument to SED. SED can apply each command on the pattern buffer. The following example illustrates the second form of SED.
First, create a text file containing SED commands. For easy understanding, let us use the same SED commands.
[jerry]$ echo -e "1d\n2d\n5d" > commands.txt 
[jerry]$ cat commands.txt
On executing the above code, you get the following result:
1d 
2d 
5d 
Now instruct the SED to read commands from the text file. Here, we achieve the same result as shown in the above example.
[jerry]$ sed -f commands.txt books.txt
On executing the above code, you get the following result:
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
6) A Game of Thrones,George R. R. Martin, 864 

Standard Options

SED supports the following standard options:
  • -n: Default printing of pattern buffer. For example, the following SED command does not show any output:
  • [jerry]$ sed -n '' quote.txt 
    
  • -e : Next argument is an editing command. Here, angular brackets imply mandatory parameter. By using this option, we can specify multiple commands. Let us print each line twice:
  • [jerry]$ sed -e '' -e 'p' quote.txt
    
On executing the above code, you get the following result:
There is only one thing that makes a dream impossible to achieve: the fear of failure. 
There is only one thing that makes a dream impossible to achieve: the fear of failure. 
 - Paulo Coelho, The Alchemist 
 - Paulo Coelho, The Alchemist
  • -f : Next argument is a file containing editing commands. The angular brackets imply mandatory parameter. In the following example, we specify print command through file:
[jerry]$ echo "p" > commands 
[jerry]$ sed -n -f commands quote.txt
On executing the above code, you get the following result:
There is only one thing that makes a dream impossible to achieve: the fear of failure. 
 - Paulo Coelho, The Alchemist

No comments:

Post a Comment