Guide to UNIX Using Linux (4th Edition) 1. What is sed? A Stream editor is used
ID: 3818586 • Letter: G
Question
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.
a. Use sed to find all the lines containing our search pattern (in this case, “erors”). Use the p to obtain the result (show the command and corresponding results below):
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
b. Using just sed and p in the command above prints the entire file, but the lines containing the search string are printed ________. This is not what is wanted. In order to only print those lines matching the pattern, use the -n option (again, show the command and corresponding results below):
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
c. Using the same example text file, show the sed command to use to see the lines that do NOT contain the search string (show the command and corresponding results below):
d. Now, you want to take out the lines containing the errors. In the example file, these are lines 2–4. Specify this range to address together with the d command (show the command and corresponding results below):
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
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:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Explanation / Answer
I am answering first 4 parts of this question, ie. a,b,c & d, with detailed descriptions so as to make you understand how sed works. Go through the complete answer, understand the working and work on other parts as practice to completely learn the concepts.
a.) Use sed to find all the lines containing our search pattern (in this case, “erors”). Use the p to obtain the result (show the command and corresponding results below):
ANS: Use p option to print the text with search pattern 'erors' as follows:
COMMAND: sed '/erors/p' ~/example
OUTPUT:
This is the first line of an example text.
It is a text with erors.
It is a text with erors.
Lots of erors.
Lots of erors.
So much erors, all these erors are making me sick.
So much erors, all these erors are making me sick.
This is a line not containing any errors.
This is the last line.
b.) Using just sed and p in the command above prints the entire file, but the lines containing the search string are printed ________. This is not what is wanted. In order to only print those lines matching the pattern, use the -n option (again, show the command and corresponding results below):
ANS: Use n option to print in silent mode, i.e. do not print the whole file and only print the lines with the text with search pattern 'erors' as follows:
COMMAND: sed -n '/erors/p' ~/example
OUTPUT:
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.
c.) Using the same example text file, show the sed command to use to see the lines that do NOT contain the search string (show the command and corresponding results below):
ANS: Use !p option to ignore the lines with search pattern 'erors' and instead print other lines as follows:
COMMAND: sed -n '/erors/!p' ~/example
OUTPUT:
This is the first line of an example text.
This is a line not containing any errors.
This is the last line.
d.) Now, you want to take out the lines containing the errors. In the example file, these are lines 2–4. Specify this range to address together with the d command (show the command and corresponding results below):
ANS: Use d option along with start and end line numbers of the range of lines to be deleted and store the results in a new file as follows.:
COMMAND: sed '2,4d' ~/example > ~/example_without_erors
OUTPUT: The file example_without_erors will now be created and contain the text as follows:
This is the first line of an example text.
This is a line not containing any errors.
This is the last line.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.