Course / The terminal and the language
Replace words with sed
How to fetch and submit this lab
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach lab: download it, edit the file named in the instructions, then submit.
gonomicon fetch week-1/03-03-sed-substitute-words
cd 03-03-sed-substitute-words
gonomicon submitGoal
The file testdata/app.log mentions timeout in several places. Print the whole file with every occurrence of timeout replaced by TIMEOUT, and everything else left exactly as it is.
Example
If testdata/app.log held:
info all fine
warn timeout on db
error timeout, then another timeout
your script prints:
info all fine
warn TIMEOUT on db
error TIMEOUT, then another TIMEOUT
Notice that the first line had no match and still gets printed, and that the last line had two matches and both changed.
Rules
- Edit
answer.sh. Readtestdata/app.logand print the result to the screen. - Print every line of the file, in the original order, including lines with no match.
- Replace every match on a line, not just the first one.
- Matching is case-sensitive: only the lower-case word
timeoutchanges. A word likeTimeoutorTIMEOUTstays as it is. - Change only the matching text. Any other characters on the line stay untouched, and a longer word that contains
timeout(such astimeouts) still has itstimeoutpart replaced. - Do not edit
testdata/app.logitself. Your script only prints the changed text.
What you'll practice
sed 's/old/new/g' and the difference between replacing the first match and replacing every match.
Run it
gonomicon fetch week-1/03-03-sed-substitute-words
cd 03-03-sed-substitute-words
gonomicon test
gonomicon submit
Hints
sedrewrites text as it flows through. Thescommand means substitute:s/old/new/.- Without the trailing
g, sed only changes the first match on each line, and some lines here have two. - The shape is
sed 's/OLD/NEW/g' FILE. Put the file path last.