SD 212 Spring 2024 / Homeworks


hw10: Word swap

  • Due before the beginning of class on Wednesday, January 31

Review

Make sure you understand these concepts:

  • Regex basics (literals, metacharacters, word boundary)
  • Using the sed command to do simple substitutions
  • Bash pipelines

Your task

Write a bash script called winlose.sh that reads in any text input and transforms it as follows: If any word starts with “win”, those letters are replaced by “lose”, and similarly for any word that starts with “lose” those letters are replaced with “win”.

For example, if this input is typed on the command line when running your program:

It doesn't matter if you win or lose.
Either way, you are a winner.
If the game is close, instead of bowing out,
keep fighting and win, win, win!

Then the output of your program would be:

It doesn't matter if you lose or win.
Either way, you are a losener.
If the game is close, instead of bowing out,
keep fighting and lose, lose, lose!

Hints:

  • Your script is supposed to read from standard in and write to standard out. So you probably shouldn’t be specifying any filenames in your commands.

  • I don’t think you can do this with a single sed substitution. But you can do multiple substitutions, one after another, in a pipeline!

  • You might want to use some strange word that would never appear in real text as a “temporary” substitution, like “XXXXXX” or “@@@TEMP@@@”.

Submit command

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

submit -c=sd212 -p=hw10 winlose.sh
club -csd212 -phw10 winlose.sh