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

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 24. 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: ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!