Question

In: Computer Science

Guide to UNIX Using Linux (4th Edition) 1.What is sed? A Stream editor is used to...

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

Solutions

Expert Solution

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.


Related Solutions

guide to unix using linux / 4th edition / chapter 2 / p10 2.10 Assume that...
guide to unix using linux / 4th edition / chapter 2 / p10 2.10 Assume that you work for a company that is developing a telephone database and you are creating directories for the Mail and Receiving Departments, which are referenced in the company’s budget and accounting systems as departments 4540 and 4550. After you create the directories, you begin creating files of department phone numbers to store in those directories. You must use the mkdir (make directory) command to...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and add pictures of the machine.) Project 2-6 In this hands-on project, you properly shut down your Linux system. 1. Press CtrlþAltþF2 to switch to a command-line terminal (tty2), and then log in to the terminal using the user name of root and the password of LNXrocks!. 2. At the command prompt, type poweroff to shut down your Linux system immediately. Which commands from Table...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and add pictures of the machine.) Project 2-3 In this hands-on project, you log in to a graphical terminal in Fedora Linux and interact with the GNOME and KDE desktops. 1. Switch to the graphical terminal (tty1) by pressing CtrlþAltþF1, click user1, supply the password of LNXrocks!, and click Sign In. Which desktop is started and why? 2. The first time you log into the...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and add pictures of the machine.) Project 2-5 In this hands-on project, you find information about commands using help utilities. 1. Press CtrlþAltþF2 to switch to a command-line terminal (tty2), and then log in to the terminal using the user name of root and the password of LNXrocks!. 2. At the command prompt, type man –k cron and press Enter to view a list of...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and add pictures of the machine.) Project 2-4 In this hands-on project, you use and protect shell metacharacters. 1. Switch to a command-line terminal (tty2) by pressing CtrlþAltþF2 and log in to the terminal using the user name of root and the password of LNXrocks!. 2. At the command prompt, type date;who and press Enter to run the date command immediately followed by the who...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and add pictures of the machine.) Project 2-2 In this hands-on project, you explore some command-line terminals on a Linux system and enter some basic commands into the BASH shell. 1. After your Linux system has been loaded, you are placed at a graphical terminal (tty1). Instead of logging in to this graphical terminal, press CtrlþAltþF2 to switch to a command-line terminal (tty2) and then...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and add pictures of the machine.) Project 4-6 In this hands-on project, you delete files and directories using the rmdir and rm commands. 1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!. 2. At the command prompt, type cd samples and press Enter. At the command prompt,...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it in a mac and add pictures of the machine.) Project 4-7 In this hands-on project, you apply and modify access permissions on files and directories and test their effects. 1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!. 2. At the command prompt, type touch permsample and press Enter. Next,...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it on mac and please...
CompTIA Linux+ Guide to Linux Certification (4th Edition)(Please do not do it on mac and please add photos of the virtual machine.) Project 4-11 In this hands-on project, you configure and research filesystem attributes. 1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!. 2. At the command prompt, type touch toughfile and press Enter. Next, type lsattr toughfile at the command prompt...
Unix / Linux 6. vi editor: In vi command mode, what are the keystrokes you would...
Unix / Linux 6. vi editor: In vi command mode, what are the keystrokes you would type to... Quit the vi editor and save any changes? 7. C language source files, by convention, have the suffix ".c". The associated "header" files have the suffix ".h". What is the command to perform the following task: List all the C source and C header files in the current directory. [Hidden files may remain hidden.] 8. C language source files, by convention, have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT