Raj
1
I am pretty sure I used grep command many times to grep two strings in Linux. See here is what I am trying:
dnf search pacakge | grep -i "string1|string2"
dnf search nginx | grep 'nginxnginx-mod-mail|nginx-mod-stream'
How do you grep 2 strings in CentOS Linux? What am I missing?
monk
2
The syntax is invalid. Try escaping it
grep 'word-1\|word-2'
grep 'string-1\|string-2\|string-3'
grep [options] 'word-1\|word-2'
grep -i -w 'word-1\|word-2' fillename
command | grep -i -w 'word-1\|word-2'
## your dnf example with shell pipes ##
dnf search nginx | grep 'nginx-mod-mail\|nginx-mod-stream'
You should see something as follows but package version may differ as I am on Fedora:
nginx-mod-mail.x86_64 : Nginx mail modules
nginx-mod-stream.x86_64 : Nginx stream modules
The egrep command has much simpler syntax, but the grep works too as pointed out by @monk:
dnf search nginx | egrep 'nginx-mod-mail|nginx-mod-stream'
Fyi, you have typo too grep '**nginxnginx**-mod-mail|nginx-mod-stream'
as nginxnginx
. It should be a single word nginx
, however you still need to the \|
with grep.
Look into our guides:
https://www.cyberciti.biz/faq/searching-multiple-words-string-using-grep/
And
https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
Pourko
4
That needs extended regex, so use grep -E
(or egrep, which is basically an alias to grep -E)
[...] | grep -iE "string1|string2|string3"