Shell find and replace
Shell find and replace
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:
Command Explanation
8 Goto line 8, address of line.
s Substitute
/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".
Command Explanation
:1,$ Substitute for all line
s Substitute
LSST v1.05 > Chapter 6 > Find and Replace (Substituting regular expression)
http://www.cyberciti.biz/pdf/lsst/ch06sec06.html (1 of 3) [7/29/2002 6:53:31 PM]
/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.
LSST v1.05 > Chapter 6 > Find and Replace (Substituting regular expression)
http://www.cyberciti.biz/pdf/lsst/ch06sec06.html (2 of 3) [7/29/2002 6:53:31 PM]
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
LSST v1.05 > Chapter 6 > Find and Replace (Substituting regular expression)
http://www.cyberciti.biz/pdf/lsst/ch06sec06.html (3 of 3) [7/29/2002 6:53:31 PM]
Linux Shell Scripting Tutorial (LSST) v1.05r3
Prev Chapter 6: Learning expressions with ex Next
Replacing word with confirmation from
user
Give command as follows
:g/Linux/ s//UNIX/gc
After giving this command ex will ask you question like - replace with UNIX
(y/n/a/q/^E/^Y)?
Type y to replace the word or n to not replace or a to replace all occurrence of
word.
Prev Home Next
Find and Replace (Substituting regular
expression)
Up Finding words
LSST v1.05 > Chapter 6 > Replacing word with confirmation from user
http://www.cyberciti.biz/pdf/lsst/ch06sec07.html [7/29/2002 6:53:32 PM]
Linux Shell Scripting Tutorial (LSST) v1.05r3
Prev Chapter 6: Learning expressions with ex Next
Finding words
Command like
:g/the/p
It is different from all other Os
My brother Vikrant also loves linux who also loves unix.
Will find word like theater, the, brother, other etc. What if you want to just find
the word like "the" ? To
find the word (Let's say Linux) you can give command like
:/\<Linux\>
Linux is cooool.
:g/\<Linux\>/p
Linux is cooool.
Linux is now 10 years old.
Rani my sister never uses Linux
The symbol \< and \> respectively match the empty string at the beginning and end
of the word. To find
the line which contain Linux pattern at the beginning give command
:/^Linux
Linux is cooool.
As you know $ is end of line character, the ^ (caret) match beginning of line. To
find all occurrence of
pattern "Linux" at the beginning of line give command
:g/^Linux
Linux is cooool.
Linux is now 10 years old.
And if you want to find "Linux" at the end of line then give command
:/Linux $
Rani my sister never uses Linux
Following command will find empty line:
:/^$
To find all blank line give command:
:g/^$
To view entire file without blank line you can use command as follows:
:g/[^/^$]
Hello World.
This is vivek from Poona.
I love linux.
It is different from all other Os
LSST v1.05 > Chapter 6 > Finding words
http://www.cyberciti.biz/pdf/lsst/ch06sec08.html (1 of 3) [7/29/2002 6:53:33 PM]
My brother Vikrant also loves linux who also loves unix.
He currently learn linux.
Linux is cooool.
Linux is now 10 years old.
Next year linux will be 11 year old.
Rani my sister never uses Linux
She only loves to play games and nothing else.
Do you know?
. (DOT) is special command of linux.
Okay! I will stop.
Command Explanation
g All occurrence
/[^ [^] This means not
/^$
Empty line, Combination of ^
and $.
To delete all blank line you can give command as follows
:g/^$/d
Okay! I will stop.
:1,$ p
Hello World.
This is vivek from Poona.
I love linux.
It is different from all other Os
My brother Vikrant also loves linux who also loves unix.
He currently learn linux.
Linux is cooool.
Linux is now 10 years old.
Next year linux will be 11 year old.
Rani my sister never uses Linux
She only loves to play games and nothing else.
Do you know?
. (DOT) is special command of linux.
Okay! I will stop.
Try u command to undo, to undo what you have done it, give it as follows:
:u
:1,$ p
Hello World.
This is vivek from Poona.
....
...
....
Okay! I will stop.
LSST v1.05 > Chapter 6 > Finding words
http://www.cyberciti.biz/pdf/lsst/ch06sec08.html (2 of 3) [7/29/2002 6:53:33 PM]
Prev Home Next
Replacing word with confirmation from
user
Up Using range of characters in regular
expressions
LSST v1.05 > Chapter 6 > Finding words
http://www.cyberciti.biz/pdf/lsst/ch06sec08.html (3 of 3) [7/29/2002 6:53:33 PM]
Linux Shell Scripting Tutorial (LSST) v1.05r3
Prev Chapter 6: Learning expressions with ex Next
Using range of characters in regular
expressions
Try the following command
:g/Linux/p
Linux is cooool.
Linux is now 10 years old.
Rani my sister never uses Linux
This will find only "Linux" and not the "linux", to overcome this problem try as
follows
:g/[Ll]inux/p
I love linux.
My brother Vikrant also loves linux who also loves unix.
He currently learn linux.
Linux is cooool.
Linux is now 10 years old.
Next year linux will be 11 year old.
Rani my sister never uses Linux
. (DOT) is special command of linux.
Here a list of characters enclosed by [ and ], which matches any single character
in that range. if the first
character of list is ^, then it matches any character not in the list. In above
example [Ll], will try to match
L or l with rest of pattern. Let's see another example. Suppose you want to match
single digit character in
range you can give command as follows
:/[0123456789]
Even you can try it as follows
:g/[0-9]
Linux is now 10 years old.
Next year linux will be 11 year old.
Here range of digit is specified by giving first digit (0-zero) and last digit (1),
separated by hyphen. You
can try [a-z] for lowercase character, [A-Z] for uppercase character. Not just
this, there are certain named
classes of characters which are predefined. They are as follows:
Predefined
classes of
characters
Meaning
[:alnum:] Letters and Digits (A to Z or a to z or 0 to 9)
[:alpha:] Letters A to Z or a to z
[:cntrl:] Delete character or ordinary control character (0x7F or 0x00 to 0x1F)
LSST v1.05 > Chapter 6 > Using range of characters in regular expressions
http://www.cyberciti.biz/pdf/lsst/ch06sec09.html (1 of 3) [7/29/2002 6:53:35 PM]
[:digit:] Digit (0 to 9)
[:graph:] Printing character, like print, except that a space character is excluded
[:lower:] Lowercase letter (a to z)
[:print:] Printing character (0x20 to 0x7E)
[:punct:] Punctuation character (ctrl or space)
[:space:]
Space, tab, carriage return, new line, vertical tab, or form feed (0x09
to 0x0D, 0x20)
[:upper:] Uppercase letter (A to Z)
[:xdigit:] Hexadecimal digit (0 to 9, A to F, a to f)
For e.g. To find digit or alphabet (Upper as well as lower) you will write
:/[0-9A-Za-Z]
Instead of writing such command you could easily use predefined classes or range as
follows
:/[[:alnum:]]
The . (dot) matches any single character.
For e.g. Type following command
:g/\<.o\>
She only loves to play games and nothing else.
Do you know?
This will include lo(ves), Do, no(thing) etc.
* Matches the zero or more times
For e.g. Type following command
:g/L*
Hello World.
This is vivek from Poona.
....
....
:g/Li*
Linux is cooool.
Linux is now 10 years old.
Rani my sister never uses Linux
:g/c.*and
. (DOT) is special command of linux.
Here first c character is matched, then any single character (.) followed by n
number of single character
(1 or 100 times even) and finally ends with and. This can found different word as
follows command or
catand etc.
In the regular expression metacharacters such as . (DOT) or * loose their special
meaning if we use as \.
or \*. The backslash removes the special meaning of such meatcharacters and you can
use them as
ordinary characters. For e.g. If u want to search . (DOT) character at the
beginning of line, then you can't
LSST v1.05 > Chapter 6 > Using range of characters in regular expressions
http://www.cyberciti.biz/pdf/lsst/ch06sec09.html (2 of 3) [7/29/2002 6:53:35 PM]
use command as follows
:g/^.
Hello World.
This is vivek from Poona.
....
..
...
. (DOT) is special command of linux.
Okay! I will stop.
Instead of that use
:g/^\.
. (DOT) is special command of linux.
Prev Home Next
Finding words Up Using & as Special replacement character