Stream Editor (SED)


Download Stream Editor (SED)


Preview text

Stream Editor i

Stream Editor
About the Tutorial
This tutorial takes you through all about Stream EDitor (SED), one of the most prominent text-processing utilities on GNU/Linux. Similar to many other GNU/Linux utilities, it is stream-oriented and uses simple programming language. It is capable of solving complex text processing tasks with few lines of code. This easy, yet powerful utility makes GNU/Linux more interesting.
Audience
If you are a software developer, system administrator, or a GNU/Linux loving person, then this tutorial is for you.
Prerequisites
You must have basic understanding of GNU/Linux operating system and shell scripting.
Copyright & Disclaimer
 Copyright 2014 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected]
i

Stream Editor
Table of Contents
About the Tutorial....................................................................................................................................i Audience ..................................................................................................................................................i Prerequisites ............................................................................................................................................i Copyright & Disclaimer.............................................................................................................................i Table of Contents ....................................................................................................................................ii
1. SED ─ OVERVIEW.................................................................................................................1
Typical Uses of SED .................................................................................................................................1
2. SED ─ ENVIRONMENT..........................................................................................................2
Installation Using Package Manager........................................................................................................2 Installation from Source Code .................................................................................................................3
3. SED ─ WORKFLOW...............................................................................................................5 4. SED ─ BASIC SYNTAX............................................................................................................7
Standard Options ....................................................................................................................................8 GNU Specific Options ..............................................................................................................................9
5. SED ─ LOOPS......................................................................................................................10 6. SED ─ BRANCHES ...............................................................................................................12 7. SED ─ PATTERN BUFFER.....................................................................................................14 8. SED ─ PATTERN RANGE......................................................................................................18 9. SED ─ BASIC COMMANDS..................................................................................................20
Delete Command ..................................................................................................................................20 Write Command....................................................................................................................................21 Append Command ................................................................................................................................23 Change Command .................................................................................................................................25
ii

Stream Editor
Insert Command....................................................................................................................................26 Translate Command ..............................................................................................................................27 l command ............................................................................................................................................28 Quit Command......................................................................................................................................29 Read Command.....................................................................................................................................30 Execute Command ................................................................................................................................32 Miscellaneous Commands.....................................................................................................................34
10. SED ─ SPECIAL CHARACTERS..............................................................................................37
= Command...........................................................................................................................................37 & Command ..........................................................................................................................................39
11. SED ─ STRINGS...................................................................................................................40
Substitute Command.............................................................................................................................40 Creating a Substring ..............................................................................................................................43 String Replacement Flags (GNU SED only) .............................................................................................44
12. SED ─ MANAGING PATTERNS ............................................................................................46
13. SED ─ REGULAR EXPRESSIONS ...........................................................................................51
Standard Regular Expressions ...............................................................................................................51 POSIX Classes of Regular Expressions ....................................................................................................57 Metacharacters .....................................................................................................................................58
14. SED ─ USEFUL RECIPES ......................................................................................................61
Cat Command........................................................................................................................................61 Removing Empty Lines ..........................................................................................................................61 Removing Commented Lines from a C++ Program.................................................................................62 Adding Comments Before Certain Lines ................................................................................................63 Wc -l command .....................................................................................................................................63 Head Command.....................................................................................................................................63
iii

Stream Editor Tail -1 Command ...................................................................................................................................64 Dos2unix Command ..............................................................................................................................64 Unix2dos command ..............................................................................................................................65 Cat -E command ....................................................................................................................................66 Cat -ET Command..................................................................................................................................66 nl Command..........................................................................................................................................67 cp Command .........................................................................................................................................67 Expand Command .................................................................................................................................67 Tee Command .......................................................................................................................................68 cat -s Command ....................................................................................................................................68 grep Command......................................................................................................................................69 grep -v Command..................................................................................................................................69 tr Command ..........................................................................................................................................70
iv

1. SED ─ Overview

Stream Editor

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.

1

Stream Editor
2. SED ─ 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 --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. 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
2

Stream Editor
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 utilitywget 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 sedcommand as follows:
3

Stream Editor
[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.
4

3. SED ─ Workflow Stream Editor
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.
 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-aplace. We will explore about it in later sections.  There is another memory area called hold buffer which is also private, inmemory, 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.
5

Preparing to load PDF file. please wait...

0 of 0
100%
Stream Editor (SED)