Use of dot “.” in regular expressions during search/grep in bash shell linux ?

Today we will look at the usage of dot “.” on Linux bash shell and how to use it in shell scripts to match a pattern.

I. Usage of “.” dot in bash shell linux

1. First create a sample file like below.
[root@nglinux ]# cat testfile 
## hello, this is comment
## welcome to ngelinux

Tomatoes are 10$
Onion is 15$

[root@nglinux ]# 


2. Now grep "." from the file.
It will remove all /n or newline characters and display only lines with text.

[root@nglinux ]# cat testfile | grep .
## hello, this is comment
## welcome to ngelinux
Tomatoes are 10$
Onion is 15$
[root@nglinux ]# 


3. Lets grep lines with text and having # in it.

[root@nglinux ]# cat testfile | grep .\#
## hello, this is comment
## welcome to ngelinux
[root@nglinux ]# 


4. Similarly we can grep $ symbol lines.

[root@nglinux ]# cat testfile | grep .\\$
Tomatoes are 10$
Onion is 15$
[root@nglinux ]# 

 

II. Use of . to search for one character

### grep linux text preceding with a character matched by .
[root@nglinux testdir]# cat testfile | grep .linux
## welcome to ngelinux
[root@nglinux testdir]# 

### Grep "1[some text]$" text.
[root@nglinux testdir]# cat testfile | grep 1.\\$
Tomatoes are 10$
Onion is 15$

Similar to above examples, we can use dot “.” to grep any string from the file/text.

Leave a Reply

avatar
  Subscribe  
Notify of