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
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.

CommandExplanation
gAll 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.


Prev
Home
Next
Replacing word with confirmation from user
Up
Using range of characters in regular expressions