GREP OR: Match Any Of Multiple Patterns-Find all the lines of a file, that match any of provided patterns.
We can use grep
 and egrep
 commands:
Syntex:
grep "PATTERN1\|PATTERN2" FILE grep -E "PATTERN1|PATTERN2" FILE grep -e PATTERN1 -e PATTERN2 FILE egrep "PATTERN1|PATTERN2" FILE
Lets see it with the help of examples one by one:
I will use a log file to demonstrate above command:
abc.log is having pattern as below:
———————————————————————————————–suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………
khozbikar………………………33333……………………
1. Grep command with single search pattern:
Command:Â Â grep 1111 abc.log
output:Â techsarthi……………..1111………………………(it will print the line having search pattern)
2.Grep command with multiple search pattern:
Command:Â grep “1111\|2222” abc.log
output: (above command will print all the lines having pattern 1111 or 2222)
suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………
3. Grep command with multiple search pattern:
Command:Â grep -E “1111|2222” abc.log
output:Â (above command will print all the lines having pattern 1111 or 2222)
suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………
4.Grep command with multiple search pattern:
Command:Â grep -e 1111 -e 2222 abc.log
output:Â (above command will print all the lines having pattern 1111 or 2222)
suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………
5. egrep command with multiple search pattern:
Command: egrep “1111|2222” abc.log
output:Â (above command will print all the lines having pattern 1111 or 2222)
suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………
6.Grep command with multiple search pattern:
Command:Â grep -E “1111|2222|3333” abc.log
output:Â (above command will print all the lines having pattern 1111 or 2222 or 3333)
suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………
khozbikar………………………33333……………………