Sed - 12 * 5 points
Due

Submit via Blackboard and e-mail to me a copy berezin on hopper.

There are 11 problems in this part of the assignment.

Create a text file that contains a set of sed commands that perform the following actions. The files to be edited will be found in '/home/lx/berezin/Data'. Your command file look something like:

      #!/bin/bash

      dir=/home/hopper/berezin/Data
      acctinfo=/home/hopper/berezin/Data/acctinfo
      passwd=/home/hopper/berezin/Data/passwd

      # problem 01- change the first occurrence of a to b
      sed 's/a/b/' $passwd > sout.01

      # problem 02
      sed 's/x/y/' $passwd > sout.02

      ... etc

where sout has the problem number appended to it for each problem.

Each problem should include one or more comment lines explaining what you are doing.

You can change the contents of the data variable if you wish to run the sed commands on your own data set. But set it back before handing in the assignment. You can test your answer using my data set by using diff to compare the sout files generated with my copies to be found in '/home/lx/t09jxb2/ans'. Create a function, similar to the one outlined in the grep assignment, that invokes diff and compares my answers to yours.

      function sdiffit () {
      'diff sout.$1 /home/lx/berezin/data/sout.$1
      }

Unless stated otherwise, when a string is generically described as a word, treat a word as alpha, numeric, and underscore. If specific word given, treat as literal unless otherwise specified. If it says find the word dog, look for lower case "dog" unless the problem indicates otherwise.

Each password entry uses a : as a field delimiter.

If you spot differences between your solutions and mine, please inform me and I'll check the data sets, my solution, and the interpretation of the problem.

Write a complete sed statement that edits the specified file. Different problems use different data files. Use the appropriate variable to reference the appropriate file. You may use multiple -e options. But unless specified, do not use any other sed options.

sout.01 Substitute the 1st occurrence of the two digits 11 with eleven. These may be in the middle of a larger number. Use passwd file or $passwd.

sout.02 Substitute the 2nd occurrence of the two digits 12 with twelve. These may be in the middle of a larger number. Use passwd file or $passwd.

sout.03 Substitute all occurrences of the two digits 10 with ten. These may be in the middle of a larger number. Use passwd file or $passwd.

sout.04 Substitute 1st occurrence of word daemon with spirit. Treat it as a lower case word. Use passwd file or $passwd.

sout.05 Substitute 1st occurrence of word daemon with spirit. Treat it as a lower case word that may have its 1st character d in either upper or lower case. Keep spirit in lower case. Use passwd file or $passwd.

sout.06 If an initial is used in the gecos field and it doesn't have a period after it, add the period. If you qualify the initial as a capital letter with a space on either side, you should not need to use its field position to identify it. Use grouping around the initial to remember it before substituting. Use passwd file or $passwd.

sout.07 Swap the user id and group id fields. These are the 3rd and 4th fields separated by :. Use grouping and group all characters from 1st character up to and including 2nd :, then group uid field and trailing : and, finally, group gid field and :. Put 1st group back as is and swap 2nd and third group. Use passwd file or $passwd.

sout.08 Replace middle names with just their initial (and period) if the entry is a student id. Identify student records by looking for the 1st field to start with the letter z and a digit. Then replace the middle word in the gecos field with its 1st character. Identify the middle word as a word with a space on either side. Group the 1st letter to recall it when replacing the middle word. Use passwd file or $passwd. This may also add a period to any initial.

sout.09 indent (one tab) all lines that do not begin with a letter and a digit. Use passwd file or $passwd. A tab is just another character, just press the tab key where you wish to indicate it.

sout.10 Using the test command, substitute all but the 1st and last occurrence of the digit 1 with the string one on the line. Use passwd file or $passwd. If only one or two 1's on the line, no change will occur.

You may use an open quote and continue on successive lines to structure this :

sed 'cmd1
cmd2
cmd3' $passwd > sout.10

Remember to close the quote

Or you may use a semi-colon if multiple sed commands on the same line :

sed 'cmd1; cmd2; cmd3' $passwd > sout.10


The file acctinfo has the same info as the password file. Except that the password, uid, and gid fields have been removed and the fields are now separated by [tab]s. Remember [tab] is regular character. If you need to use it, just press the [tab] key.

I have also embedded a series of record markers, FACULTY, STUDENTS, SERVICE, and END. Lines containing one of the words will contain only that word. These mark the boundries between different login types. A marker may be repeated more than once. For the last two questions, use the acctinfo fil for $acctinfo.

sout.11 Using the -n option to suppress output, print all lines found in a block delimited by FACULTY and some marker word that is not FACULTY. Specify the range start as FACULTY on a line by itself and the range end as another word in caps on a line by itself. Use $acctinfo for data. It is ok for the marker lines to be part of the output.

sout.12 Indent by 2 spaces all lines found in a block delimited by SERVICE and some marker word that is not SERVICE. However, do not indent the marker words themselves. To do this, use the braces after the range specification to create a block of instructions that run only on the qualified range of lines. Use $acctinfo for data.

In the block, Test that the line does not contains a single capitalized word. If it doesn't, indent.