- [Instructor] Hello again and welcome to the next video
of section four, using sed to perform text replacement.
In the previous video we have learned about cutting
a file column-wise with cut.
This video will cover most of the frequently
used sed techniques.
Sed stands for stream editor, it's a very essential tool
for text processing and a marvelous utility
to play around with regular expressions.
A well known usage of the sed command
is for text replacement.
Let's see how to do it.
Sed can be used to replace occurrences
of a string with another string in a given text.
It can be matched using regular expressions.
This command reads from standard in.
If you're using the vi editor you'll notice
that the command to replace text is very similar
to the one discussed here.
By default sed only prints the substitute text,
to save your changes along with your substitutions
to the same file use the minus I option.
Most of the users follow multiple redirections
to save the file after making a replacement as follows.
However, it can be done in just one line.
For example.
These usages of the sed command will replace
the first occurrence of the pattern in each line.
If we want to replace every occurrence
we need to add the G parameter at the end as follows.
Slash G suffix means that it will substitute
every occurrence, however we sometimes need
to replace only the Nth occurrence onwards.
For this we can use the slash capital NG form of the option.
Have a look at the following commands.
We've used slash in sed as a delimiter character,
we can use any delimiter character as follows.
When the delimiter character appears inside the pattern
we have to escape it using the slash prefix as follows.
Slash pipeline is a delimiter appearing in the pattern
replaced with escape.
The sed command comes with numerous options
for text manipulation by combining the options
available with sed in logical sequences
many complex problems can be solved in one line.
Let's see the different options available with sed.
Removing blank lines.
Removing blank lines is a simple technique
by using sed to remove blank lines.
Blanks can be matched with regular expression carrot dollar.
Slash pattern, slash D will remove
lines matching the pattern.
For blank lines the line end marker appears
next to the line start marker
performing replacement directly in the file.
When a file name is parsed to sed it usually prints
its output to standard out, instead if we want it
to actually modify the contents of the file
we use the minus I option as follows.
For example, replace all three digit numbers
with another specified number in a file as follows.
The preceding one line replaces
the three digit numbers only.
Slash B zero to nine, slash open curly brace three,
slash closed curly brace, slash B is the regular expression
used to match three digit numbers.
Bracket zero to nine is the range of digits
that is from zero to nine, curly brackets three
is used for matching the preceding character thrice.
Slash in slash three is used to give a special meaning
for curly brackets and slash B is the word boundary marker.
It's useful to practice to first try the sed command
without the minus I to make sure your regex is correct
and once you're satisfied with the results
add the minus I option to actually make
the change to the file.
Alternatively you can use the following form of sed.
In this case sed will not only perform
the replacement on the file, but it will also
create a file called file dot back
which will contain the original contents.
Matched string notation.
In sed we can use ampersand as the matched string
for the substitution pattern in such a way
that we can use the match string in the replacement string.
For example, here regex this matches every word,
then we replace it with this.
This corresponds to the word that is matched.
Substring match notation and operator
corresponds to the match string of a given pattern.
We could also match the substrings with a given pattern.
Let's see how to do it.
The preceding command replaces digit seven with seven.
The substring matched is seven, slash brackets pattern slash
is used to match the substring.
If the pattern is enclosed in brackets
as is scope for the slashes.
For the first substring match the corresponding notation
is slash one, for the second it is slash two and so on.
Go through the following example with multiple matches.
This match is the first word and this match
is the second word.
Slash one and slash two are used for referencing them.
This type of referencing is called back referencing.
In the replacement part the order is changed
as slash two, slash one and hence it appears
in reverse order.
Combination of multiple expressions.
The combination of multiple sed using a pipe
can be replaced as follows.
The preceding command is equivalent to the following.
For example.
Quoting, usually it is seen that the sed expression
is quoted by using single quotes,
but double quotes can also be used.
Double quotes expand the expression by evaluating it.
Using double quotes are useful
when we want to use a variable string in a sed expression.
For example, this is evaluated as hello.
Awesome, we've successfully learned
about using sed to perform text replacement.
In the next video we'll learn how to use awk
for advanced text processing.