Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep egrep multiple-strings

Tags:

regex

grep

Suppose I have several strings: str1 and str2 and str3.

  • How to find lines that have all the strings?
  • How to find lines that can have any of them?
  • And how to find lines that have str1 and either of str2 and str3 [but not both?]?
like image 678
Tim Avatar asked Oct 03 '09 20:10

Tim


People also ask

Can grep have multiple strings?

Grep is a powerful utility available by default on UNIX-based systems. The name stands for Global Regular Expression Print. By using the grep command, you can customize how the tool searches for a pattern or multiple patterns in this case. You can grep multiple strings in different files and directories.

How do I grep for a string in multiple files in a directory?

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.

Is grep faster than egrep?

Example: Note: The egrep command used mainly due to the fact that it is faster than the grep command. The egrep command treats the meta-characters as they are and do not require to be escaped as is the case with grep.


2 Answers

This looks like three questions. The easiest way to put these sorts of expressions together is with multiple pipes. There's no shame in that, particularly because a regular expression (using egrep) would be ungainly since you seem to imply you want order independence.

So, in order,

  1. grep str1 | grep str2 | grep str3

  2. egrep '(str1|str2|str3)'

  3. grep str1 | egrep '(str2|str3)'

you can do the "and" form in an order independent way using egrep, but I think you'll find it easier to remember to do order independent ands using piped greps and order independent or's using regular expressions.

like image 186
groundhog Avatar answered Sep 19 '22 12:09

groundhog


You can't reasonably do the "all" or "this plus either of those" cases because grep doesn't support lookahead. Use Perl. For the "any" case, it's egrep '(str1|str2|str3)' file.

The unreasonable way to do the "all" case is:

egrep '(str1.*str2.*str3|str3.*str1.*str2|str2.*str1.*str3|str1.*str3.*str2)' file

i.e. you build out the permutations. This is, of course, a ridiculous thing to do.

For the "this plus either of those", similarly:

egrep '(str1.*(str2|str3)|(str2|str3).*str1)' file
like image 43
chaos Avatar answered Sep 17 '22 12:09

chaos