In: Computer Science
Guide to UNIX Using Linux (4th Edition)
1.What is sed?
A Stream editor is used to perform basic transformations on text
read from a file or a pipe. The result is sent to standard output.
The syntax for the sed command has no output file
specification, but results can be saved to a file using output
redirection. The editor does not modify the original input.
What distinguishes sed from other editors, such
as vi and ed, is its ability to
filter text that it gets from a pipeline feed. You do not need to
interact with the editor while it is running; that is why
sed is sometimes called a batch editor. This
feature allows use of editing commands in scripts, greatly easing
repetitive editing tasks. When facing replacement of text in a
large number of files, sed is a great help.
The sed program can perform text pattern
substitutions and deletions using regular expressions like the ones
used with the grep command. The editing commands
are similar to the ones used in the vi editor:
Command Result
a\ Append text below current line
c\ Change text in the current line with new text
d Delete text
i\ Insert text above current line
p Print text
r Read a file
s Search and replace text
w Write to a file
Apart from editing commands, you can give options to sed. An overview is in the table below:
Option Effect
-e SCRIPT Add the commands in SCRIPT to the set of commands to be run while processing the input
-f Add the commands contained in the file SCRIPT-FILE to the set of commands to be run while processing the input
-n Silent mode
-V Print version information and exit
The sed info pages contain more information;
only list the most frequently used commands and options here.
Printing lines containing a pattern is something you can do with
grep, of course; however, you cannot do a “find
and replace” using that command. This is the example text file:
cat -n example
1 This is the first line of an example text.
2 It is a text with erors.
3 Lots of erors.
4 So much erors, all these erors are making me sick.
5 This is a line not containing any errors.
6 This is the last line.
e. Next, print the first 2 lines of the original example file (show the command and corresponding results below):
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
f. Display the command and corresponding results that print the first line containing the pattern ” up to and including the next line containing the pattern :
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
g. In the example file, now search (using s only) and replace the errors instead of only (de)selecting the lines containing the search string (show the command and corresponding results below):
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
h. What specifically happens to line 4 in this file? How does
using the using g command fix the problem? (show
the command and the corresponding results below)
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
i. Now, you want to add a > character to the beginning of each line for quoting (show the command and the corresponding results below that will do this):
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
j. Likewise, you want to add EOL to the end of each line in the
example file (show the command and the corresponding results below
that will do this):
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
k. Now, use the –e
option to include 2 replace commands for the example file: 1 to
replace all “erors” with “errors” and 1 to replace all last words
with “final.” Show the command and the corresponding results below
that will do this:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
e) Let us consider that the given "example" file is a text file i.e., example.txt.
Here we use the "sed" command and along with it we use "-n" option to print the desired lines.
Syntax: [user.linux]@ sed -n 'x,y'p file_name
[user.linux]@ sed -n '1,2'p example.txt
1 This is the first line of an example text.
2 It is a text with erors.
f) To print the patterns we use the "-n" option along with the sed command.
Syntax: [user.linux]@ sed -n /pattern/p filename
To print the pattern to a specied line the syntax is:
[user.linux]@ sed -n '/pattern/np' filename
[user.linux]@ sed -n '/erors/2p' example.txt # here we take the pattern as "erors"
2. It is a text with erors.
Here only the second line is printed as the first line doesn't have the matching pattern string and we are printing only till the second line. So only the second line is printed.
g) To replace the string in the file we use the "s" symbol which specifies the substitution. Along with it we use the "/g" which indicates global substitution.
[user.linux]@ sed 's/erors/errors/g' example.txt
1 This is the first line of an example text.
2 It is a text with errors.
3 Lots of errors.
4 So much errors, all these errors are making me sick.
5 This is a line not containing any errors.
6 This is the last line.
h) Since the 4th line contains two "erors" strings, while replacing those strings only the string which occurs first will be replaced and the later one's remains the same.
Using the "g" specifier we replace all the strings in that line.
[user.linus]@ sed 's/erors/errors' example.txt
1 This is the first line of an example text.
2 It is a text with errors.
3 Lots of errors.
4 So much errors, all these erors are making me sick.
5 This is a line not containing any errors.
6 This is the last line.
[user.linux]@ sed 's/erors/errors/g' example.txt
1 This is the first line of an example text.
2 It is a text with errors.
3 Lots of errors.
4 So much errors, all these errors are making me sick.
5 This is a line not containing any errors.
6 This is the last line.
i) To add text or any character at the beginning of each line of the file we use the keyword "PREFIX" along with the sed command.
Syntax: [user.linux]@ sed 's/character/PREFIX/' filename
[user.linux]@ sed 's/>/PREFIX/' example.txt
> 1 This is the first line of an example text.
> 2 It is a text with erors.
> 3 Lots of erors.
> 4 So much erors, all these erors are making me sick.
> 5 This is a line not containing any errors.
> 6 This is the last line.
j) Similarly in order to add any character or text at the end of each line, we use the keyword "SUFFIX" along with the sed command.
Syntax: [user.linux]@ sed 's/character/SUFFIX/' filename
[user.linux]@ sed 's/EOL/SUFFIX/' example.txt
1 This is the first line of an example text. EOL
2 It is a text with erors. EOL
3 Lots of erors. EOL
4 So much erors, all these erors are making me sick. EOL
5 This is a line not containing any errors. EOL
6 This is the last line. EOL
k) We use the "-e" to do multiple operations in a single line using the sed command.
[user.linux]@ sed -e 's/erors/errors/g' -e 's/[^ ]* *$/final' example.txt
1 This is the first line of an example text.
2 It is a text with final
3 Lots of final
4 So much errors, all these errors are making me final
5 This is a line not containing any final
6 This is the last final
The line "s/[^ ]* *$/final" initially navigates the compiler to the end of the line and then replaces the last word with final.