- Sed - Useful Recipes
- Sed - Regular Expressions
- Sed - Managing Patterns
- Sed - Strings
- Sed - Special Characters
- Sed - Basic Commands
- Sed - Pattern Range
- Sed - Pattern Buffer
- Sed - Branches
- Sed - Loops
- Sed - Basic Syntax
- Sed - Workflow
- Sed - Environment
- Sed - Overview
- Sed - Home
Sed Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Stream Editor - Basic Syntax
This chapter introduces the basic commands that SED supports and their command-pne 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-pne 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-pne options to control its behavior.
Let us see how we can specify multiple SED commands. SED provides the delete command to delete certain pnes. Let us delete the 1st, 2nd, and 5th pnes. 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 pnes. Here, to delete three pnes, 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 2d 5d" > 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
[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
[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-sympnks: If this option is provided, the SED follows symbopc pnks 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, --pne-lenght=N: This option sets the pne 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 pne by a new-pne character. If NULL-data option is provided, it separates the pnes by NULL characters.