- 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 - 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](/sed/images/sed_workflow.jpg)
Read: SED reads a pne from the input stream (file, pipe, or stdin) and stores it in its internal buffer called pattern buffer.
Execute: All SED commands are appped sequentially on the pattern buffer. By default, SED commands are appped on all pnes (globally) unless pne 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 appped 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 pne.
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 imppes the SED command. Let us demystify this operation.
First SED reads a pne from the input file quote.txt and stores it in its pattern buffer. Then it apppes 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 pne is entered through keyboard and the second is the output generated by SED. To exit from the SED session, press ctrl-D (^D).
Advertisements