hw12: Word swap
- Due before the beginning of class on Friday, February 20
Review
Make sure you understand these concepts:
- Regex basics (literals, metacharacters, word boundary)
- Using the
sedcommand to do simple substitutions - Bash pipelines
Files
Go get started, run
git pull
as usual. You should see a new folder with this homework, with an
empty winlose.sh file for you to fill in.
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!
About sed and input
The sed command has two main modes of execution, one if you give it a filename, and another if you don’t. When you don’t give it a filename, it reads from standard in which is obviously what you want to use in this HW.
sed s/hello/hey/ myfile.txt
This will open the myfile.txt and make substitutions from the file.
sed s/hello/hey/
This will hang and wait for input. When you write your winlose.sh, you just want to include empty sed commands like this. And yes, you can pipe one sed output into another one.
Hints:
Your script is reading from standard in so you probably shouldn’t be specifying any filenames in your commands.
I don’t think you can do this with a single
sedsubstitution. 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@@@”.Remember: run your program:
bash winlose.sh
Submit command
To submit files for this homework, run one of these commands:
submit -c=sd212 -p=hw12 winlose.sh
club -csd212 -phw12 winlose.sh