Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search with grep for two expressions at once

Tags:

grep

bash

This is basic but I am unable to google it. Can I use on invokation of grep to do

grep expr1 | grep expr2 

so that it prints lines including both expr1 and expr2?

like image 259
sup Avatar asked Feb 27 '13 15:02

sup


People also ask

How do you grep 2 patterns at a time?

If you want to find exact matches for multiple patterns, pass the -w flag to the grep command. As you can see, the results are different. The first command shows all lines with the strings you used. The second command shows how to grep exact matches for multiple strings.

How do you grep twice in one line?

Replace df -h with the command you want to use, and replace head -1 and grep '/$' with the two commands you want to apply to it. The output of both will be displayed in your terminal, though it may be that the output of the former command is displayed after the latter.

Can grep search multiple files?

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.


1 Answers

You can provide several expressions to search for with several -e flags. See also this post.

i.e.

grep -e expr1 -e expr2

like image 97
rounin Avatar answered Sep 21 '22 18:09

rounin