sed assignment Fall 2016

Points :
Due

For submission, use :

In an assignment directory of your choosing, create a data file using the following command : This will generate a file of 100 lines of 0 to 8 words randomly pulled from the system's dictionary. Your zid acts as a seed value so the TA can replicate your data.

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 the results of your 1st 7 solutons by using my data set and using diff to compare the souts files generated by solutions which can be found in '/home/hopper/Sed'.

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/hopper/berezin/Sed/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.

Keep in mind, the passwd file uses a : as a field delimiter.

If you spot differences between your solutions and mine, and you think yours is rigt, check with me.

Send me your solution, I'll check it.

I may also 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 user Id, root at the beginning of the line in the psef file with Master.


sout.02 In the passwd file, substitute the portion of the home directory field that that says /home/hopper with /system/tron. Do this without using the backslash.


sout.03 In the passwd file, locate users who are daemons. This is usually signifed by the user Id ending with a d, such as printd, sshd, etc.

Once that line has been identified, append :daemon to the end of the line.

So :
sshd:x:114:65534::/var/run/sshd:/usr/sbin/nologin Will look like :
sshd:x:114:65534::/var/run/sshd:/usr/sbin/nologin:daemon

Remember : separate fields in the passord file, so using square brackets and the not, [^:], is useful.


sout.04 Copy the user Id, the 1st word/column of the line to the end of the line preceeded by a space, :, and space. Use psef

So :
z1763923 10584 10583 0 09:39 pts/24 00:00:00 -bash

Will look like :
z1763923 10584 10583 0 09:39 pts/24 00:00:00 -bash : z1763923

Check out \(\) and &. Parentheses are numbered from the left and they may be embedded. \(\(...\)....\) is legal.


sout.05 Change the 2nd occurrence of the word root in etcList to wheel.


sout.06 In the file, etcSlist, one of the columns is separated by at least 24 spaces, replace the 24 spaces with 4 spaces. Use the brace range, \{\}.


sout.07 Use -n option with sed and print only lines from passwd where the user Id, 1st column, is 4 to 6 characters long.


sout.08 Write a validity check function called NoSpace that takes the command line arguments passed it and checks for a single argument with no spaces.

In NoSpace, echo the arguments passed on the command line and pipe them to grep and pipe all output of the grep to /dev/null. All you are interested in is if the search found anywhere in the passed string.

For this one, use the -v to test for a string with no spaces.

echo "$*" | grep ....... >/dev/null 2>&1

Then return the status of the grep on returning from the NoSpace function.

If you use the -v, it should return success if no spaces found.

You could use it as follows :


sout.09 Write another validity function called IsDigits that tests that all of the characters in the input string are digits.

Don't use the -v option.

But you can start with the NoSpace function and modify it to test for a string of just 1 or more digits from start to end.


sout.10 Write another validity function called IsFloat that tests that all of the characters of a string are a floating point number. This means there must be one and only 1 period in the string and all of the other characters have to be digits.

For this one, use the -e twice.

For the 1st one, test for one or more digists, followed by a . (remember to backslash it), followed by zero or more digits.

For the 2nd one, test for zero or more digists, followed by a . (remember to backslash it), followed by one or more digits.


Some pages to review :

Regular expressions.

Sed substitution

Sed line addresssing