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

GNU Specific Options

Let us quickly go through the GNU specific SED options. Note that these options are GNU specific; and may not be supported by other variants of the SED. In later sections, we will discuss these options in more details.

  • -n, --quiet, --silent: Same as standard -n option.

  • -e script, --expression=script: Same as standard -e option.

  • -f script-file, --file=script-file: Same as standard -f option.

  • --follow-symlinks: If this option is provided, the SED follows symbolic links while editing files in place.

  • -i[SUFFIX], --in-place[=SUFFIX]: This option is used to edit file in place. If suffix is provided, it takes a backup of the original file, otherwise it overwrites the original file.

  • -l N, --line-lenght=N: This option sets the line length for l command to N characters.

  • --posix: This option disables all GNU extensions.

  • -r, --regexp-extended: This option allows to use extended regular expressions rather than basic regular expressions.

  • -u, --unbuffered: When this option is provided, the SED loads minimal amount of data from the input files and flushes the output buffers more often. It is useful for editing the output of "tail -f" when you do not want to wait for the output.

  • -z, --null-data: By default, the SED separates each line by a new-line character. If NULL-data option is provided, it separates the lines by NULL characters.

Advertisements