SD 212 Spring 2023 / Homeworks


This is the archived website of SD 212 from the Spring 2023 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

hw12: Reading about sed back-references

  • Due before the beginning of class on Monday, February 6

Today’s homework asks you to do some reading about sed and then fill in and submit a markdown file to check your understanding.

Reading

Required: Read Chapter 20 of TLCL, just the section on “Editing on the fly”. This goes through the tr command a little bit, and then goes into great detail on the sed command. We have been using sed already, but the reading here goes into some of the more powerful things which we haven’t learned about yet.

Stop reading when you get to the stuff about aspell.

Optional: [ABSG section on sed][https://tldp.org/LDP/abs/html/x23170.html]. This doesn’t go into as much detail as the required reading above, but can be a good quick reference and links to a large variety of examples using sed within broader bash contexts.

Questions

  1. Did you read the required reading, “Editing on the fly” from TLCL chapter 20?

    (Answer “yes” if you did.)

  2. Which of these commands would remove empty items from a comma-separated list, like turning “blue,green,,,orange,,yellow” into “blue,green,orange,yellow”?

    1. tr ',,' ','
    2. tr -d ',,'
    3. tr -s ','
    4. tr -e ',,' ','

    (Enter just the letter of the correct choice.)

  3. What does the following command do?

    sed 's/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/'
    1. Changes dates like 02/01/2023 to 2023-02-01
    2. Changes phone numbers like `(302) 555.0417to302-555-0417`
    3. Performs long division by doing repeated subtractions
    4. This question is impossible for mortal humans to answer
  4. Which of these commands would shorten long proper nouns to their first three letters, like Delaware would become Del. and Midshipman would become Mid.?

    1. sed -E '/\b[A-Z]\w\w(\w*)/d'
    2. sed -E 's/\b[A-Z]\w\w(\w*)/\1/./'
    3. sed -E 'y/[A-Z]\w\w\w*/[A-Z]\w\w./'
    4. sed -E 's/\b([A-Z]\w\w)\w*/\1./'

Submit command

To submit files for this homework, run one of these commands:

submit -c=sd212 -p=hw12 hw12.md 
club -csd212 -phw12 hw12.md
Download the file hw12.md to fill in and submit for this homework