Sed's Substitute Command -- Solution
Motivation
-- You often need to update text files.
The information
-- The
sed
command replaces a regular expression (R.E.) in a text file.
Positive instance
--
sed 's/cat/dog/' pets.txt
sed 's/[Cc]at/dog/' pets.txt
sed 's/cat/dog/g' pets.txt
sed 's+cat+dog+' pets.txt
Critical properties
--
Non-interactive ("batch") editing.
The 'old' text is identified with a regular expression.
Regular expressions are a powerful pattern-matching technique.
A 'g' after the last delimiter causes replacement to occur on every instance of the R.E.
The delimiter doesn't have to be a '/'.
Negative instance
-- If you omit the 'g', only the first occurrance of the R.E. in each line is replaced.
Teaching questions
--
Why might you need to update a text file?
What tools are available for this purpose?
When might you need to edit non-interactively?
When might you prefer a delimiter other than '/'?
Exercising questions
--
Write the
sed
command to replace pigs with goats in the
pets.txt
file.
What if the word 'pig' is capitalized?
What if it's plural?
Evaluating questions
--
Raise your hand if you could use
sed
to replace 'yellow' with 'purple'.
Will you use
sed
in real life? Why?
[ ] [
Course Contents
] [
Home
] [
Curriculum
]
http://dan-keller.com/training/curric/toi/example-sed-solution.html
Updated
Monday, 13-Jan-2020 15:23:57 MST
Copyright © 2020
Dan Keller Technical Services
2248 International Blvd.
Oakland, California, USA 94606
tel: +1 / 415 / 861-4500