Regex to look for multiple characters or words in linux bash shell ?

Today we will look at the regex to search/grep multiple characters or words from a file or output in linux.

First create a file with below contents.

[root@nglinux ]# cat testfile 
hello
welcome to ngelinux
first line
second line
[root@nglinux ]# 

Now lets see how to search for multiple characters or words.

1. Multiple characters

### Search for NGe and hence no result.
[root@nglinux ~]# echo "Welcome to NGELinux" | awk '/NGe/{print $0}'

### Search for NG and third character in either e or E.
[root@nglinux ~]# echo "Welcome to NGELinux" | awk '/NG[eE]/{print $0}'
Welcome to NGELinux

 

2. Multiple words

### Search for one word
[root@nglinux ]# cat testfile | awk '/line/{print $0}'
first line
second line

### Search for two words
[root@nglinux ]# cat testfile | awk '/line|hello/{print $0}'
hello
first line
second line
[root@nglinux ]# 

### Search for three different words
[root@nglinux ]# cat testfile | awk '/line|hello|welcome/{print $0}'
hello
welcome to ngelinux
first line
second line

In the similar fashion above, you can search for multiple words or characters on Linux shell.

Leave a Reply

avatar
  Subscribe  
Notify of