Linux Shell Scripting Tutorial (LSST) v1.05r3
Prev
Chapter 6: Learning expressions with ex
Next

Find and Replace (Substituting regular expression)

Give command as follows
:8 p
He currently lerarns linux.
:8 s/lerarns/learn/
:p
He currently learn linux.

Note Using above command, you are substituting the word "learn" for the word "lerarns".

Above command can be explained as follows:

CommandExplanation
8Goto line 8, address of line.
sSubstitute
/lerarns/Target pattern 
learn/If target pattern found substitute the expression (i.e. learn/ )

Considered the following command:
:1,$ s/Linux/Unix/
Rani my sister never uses Unix
:1,$ p

Hello World.
This is vivek from Poona.
....
..
.....

. (DOT) is special command of linux.

Okay! I will stop.

Using above command, you are substituting all lines i.e. s command will find all of the address line for the pattern "Linux" and if pattern "Linux" found substitute pattern "Unix".

CommandExplanation
:1,$ Substitute for all line
sSubstitute 
/Linux/Target pattern 
Unix/If target pattern found substitute the expression (i.e. Unix/ )

Even you can also use contextual address as follows
:/sister/ p
Rani my sister never uses Unix
:g /sister/ s/never/always/
:p

Rani my sister always uses Unix

Above command will first find the line containing pattern "sister" if found then it will substitute the pattern "always" for the pattern "never" (It mean find the line containing the word sister, on that line find the word never and replace it with word always.)
Try the following and watch the output very carefully.
:g /Unix/ s/Unix/Linux
3 substitutions on 3 lines

Above command finds all line containing the regular expression "Unix", then substitute "Linux" for all occurrences of "Unix". Note that above command can be also written as follows
:g /Unix/ s//Linux

Here // is replace by the last pattern/regular expression i.e. Unix. Its shortcut. Now try the following
:g /Linux/ s//UNIX/
3 substitutions on 3 lines
:g/Linux/p
Linux is cooool.
Linux is now 10 years old.
Rani my sister always uses Linux

:g /Linux/ s//UNIX/
3 substitutions on 3 lines
:g/UNIX/p

UNIX is cooool.
UNIX is now 10 years old.
Rani my sister always uses UNIX

By default substitute command only substitute first occurrence of a pattern on a line. Let's take another example, give command
:/brother/p
My brother Vikrant also loves linux who also loves unix.

Now in above line "also" word is occurred twice, give the following substitute command
:g/brother/ s/also/XYZ/
:/brother/p
My brother Vikrant XYZ loves linux who also loves unix.

Make sure next time it works
:g/brother/ s/XYZ/also/

Note that "also" is only once substituted. If you want to s command to work with all occurrences of pattern within a address line give command as follows:
:g/brother/ s/also/XYZ/g
:p
My brother Vikrant XYZ loves linux who XYZ loves unix.

:g/brother/ s/XYZ/also/g
:p
My brother Vikrant also loves linux who also loves unix.

The g option at the end instruct s command to perform replacement on all occurrences of the target pattern within a address line.


Prev
Home
Next
Searching the words
Up
Replacing word with confirmation from user