Linux egrep and or

Grep: how to add an «OR» condition? [duplicate]

How can I introduce a conditional OR into grep? Something like, grepping a file’s type for (JPEG OR JPG), and then sending only those files into the photos folder. For example. I know how to send the file where I want it, and get the file type, I just need some help with the grep part. I’m on OS X, which IMO seems to have modified/customized *nix utilities than what I’m used to in a *nix environment. So hopefully the answers can be as generic/portable as possible.

One might argue that OSX has the more *nix binaries since it is mostly BSD based. I suspect you are far more used the gnu tools.

@Zoredache Uhh .. sure! What you said! I am most familiar with Debian, though I have dabbled with Slackware, CentOS, Gentoo, Ubuntu, and a few others. Debian is my favourite, I almost can’t stand Gentoo and Ubuntu, and Slackware I find to be a bit too unfriendly for me. I tend to break stuff. Great way to learn, though.

You don’t need regexes if you just want to grep for different fixed patterns. Just use the -e parameter for each pattern you want to match, or one -F if you want to supply them as a newline separated list, or -f if you want to read them from a file (see man grep ).

5 Answers 5

You can pass multiple regexes to grep by using the -e option more than once:

grep -e regex1 -e regex2 input_file

will match lines matching regex1 or regex2 .

grep -E «ATTN|MASS|BOND|ANGLE|DIHE|IMPROPER» temp.dat and grep -e ATTN -e MASS -e BOND -e ANGLE -e DIHE -e IMPROPER temp.dat are the same.

I’m also fairly new to regex, but since noone else has answered I’ll give it a shot. The pipe-operator «|» is used for an OR operator.

The following REGEX should get you going somewhere.

(Match anything one or more times followed by either «JPG» or «JPEG» at the end)

Читайте также:  Vmware or virtualbox for kali linux

Extended answer (editted after learning a bit about (e)grep): Assuming you have a folder with following files in them:

test.jpeg, test.JpEg, test.JPEG, test.jpg, test.JPG, test.notimagefile, test.gif 

(Not that creative with names. )

First we start by defining what we know about our pattern: We know that we are looking for the end of the name. Ergo we use the «$» operand to define that each line has to end with the defined pattern. We know that the pattern needs to be either JPEG or JPG. To this we use the pipeline «|» as an or operand. Our pattern is now:

(Match any line ending with EITHER «JPEG» or «JPG»)

However we see that in this example, the only difference is the optional «E». To this we can use the «?» operand (meaning optional). We write:

(Mach any file ending with a pattern like: «J», followed by «P», followed by an optional «E», followed by a «G»).

However we might also like to match files with lowercase letters in file name. To this we introduce the character-class «[. ]». meaning match either of the following. We write:

(Match any file ending with at pattern like: «j» or «J», followed by «p» or «P», followed by an optional «e» or «E», followed by «g» or «G») (This could also be done using the «-i» option in grep, but I took this as an exercise in REGEX)

Finally, since we (hopefully) start to see a pattern, we can omit the unnecessary parentheses. Since there is only one optional letter («E»), we can omit this one. Also, since the file only has this pattern to end on, we can omit the starting and ending parenthesis. Thus we simply get:

Finally; lets assume you also want to find files with «.gif»-filetype, we can add this as a second parameter:

(Here I’ve again added extra parenthesis for readability/grouping. Feel free to remove them if they seem obfuscating.)

Finally, I used ls and a pipeline to send all file names to (e)grep:

test.gif test.JPG test.JpEg test.JPEG test.jpg test.JPG 

Second edit: Using the -i option and omitting parenthesis we can shorten it down to only:

Источник

Linux Grep OR, AND, NOT Operator and Logic Examples

lo grep is very useful tool for text search and pattern matching. We have all ready provided tutorial and examples about grep and egrep . In this tutorial we will look grep command or , and , not logic operations in detail.

Читайте также:  Astra linux special edition серверная

Example Text

We will use following text during tutorial for grep operations. This is the wage list of Manchester United Football Team.

David de Gea 26 £200,000 2 Years (2019) Sergio Romero 30 £50,000 4 Years (2021) Marcos Rojo 27 £70,000 2 Years (2019) Phil Jones 25 £50,000 2 Years (2019) Chris Smalling 27 £80,000 2 Years (2019) Eric Bailly 23 £75,000 3 Years (2020)

OR Logic

or logic matches all lines if one of the pattern match. We will use \| to specify OR logic. In this example we will look players those age 23 OR 30 .

OR Logic

OR with Extended Grep

Another way to implement OR logic is using grep command extended option with -E . We will specify OR logic with | . In this example we will look players those age 23 OR 30 .

$ grep -E "23|30" manchester.txt

OR with Extended Grep

OR with Egrep

Another tool used to implement OR logic is egrep . We will use | operator again. In this example we will look players those age 23 OR 30 .

OR with Egrep

AND Logic

AND logic will match lines those have all provided patterns. We will use .* for AND operator. In this example we will list players those contract is 2 years and age is 27.

$ grep "27.*2 Years" manchester.txt

AND Logic

AND with Multiple Grep

Another implementation is using multiple grep commands to filter given patterns. In this example we will list players those contract is 2 years and age is 27.

$ grep "2 Years" manchester.txt | grep 27

AND with Multiple Grep

NOT Logic

NOT logic is used to get results those do not matched given pattern. We will use -v option for grep. In this example we will list players those do not 27 years old.

NOT Logic

NOT with Multiple Grep Conditions

We can implement NOT logic multiple times with multiple grep commands. We will pipe multiple grep commands in bash. In this example we will list players those is not 27 years old and not have 2 Years contract.

$ grep -v "27" manchester.txt | grep -v "2 Years"

Источник

Grep “OR” Condition Tutorial

Grep

The grep command is used to filter or search or match some text or regex in a text file. The grep command provides a lot of different options and features that can be used for different cases. The grep command can be used to match one of the multiple search terms which can be accomplished with the OR logic. The OR logic is used to match one of the provided search terms. In this tutorial, we examine how to use the OR condition.

Читайте также:  Dr web linux manjaro

OR Condition without Option

The most basic implementation for the grep OR condition is using the \| characters like below.

grep "PATTERN1\|PATTERN2\|PATTERN3" FILE 
  • PATTERN1,PATTERN2 and PATTERN3 are the search terms or patterns we want to match.
  • \| characters are used to make OR condition and put between search terms or patterns.
  • FILE is the file we search.

In the following example, we match one of “ferrari”,”opel”,”audi”.

$ grep "ferrari\|opel\|audi" cars.txt

OR Condition with -E Option

The grep command OR condition can be enabled with the -E option like below.

grep -E "PATTERN1|PATTERN2|PATTERN3" FILE
  • PATTERN1,PATTERN2 and PATTERN3 are the search terms or patterns we want to match.
  • -E option is used to make search term with OR condition.
  • | character is used for OR condition

In the following example, we match one of “ferrari”,”opel”,”audi”.

$ grep -E "ferrari|opel|audi" cars.txt

OR Condition with -e Options

Another way to create OR condition with grep command is using the -e option. For every OR condition the -e option is used once and the pattern or search term is added.

grep -e PATTERN1 -e PATTERN2 -e PATTERN3 FILE 
  • PATTERN1,PATTERN2 and PATTERN3 are the search terms or patterns we want to match.
  • -e option is used to specify every condition. The -e option is used multiple times to specify multiple OR conditions.

In the following example, we match one of “ferrari”,”opel”,”audi”.

$ grep -e "ferrari" -e "opel" -e "audi" cars.txt

egrep Command OR Condition

The egrep command is a bit extended implementation of the grep command. The egrep command is the same as “grep -E”.

egrep "PATTERN1|PATTERN2|PATTERN3" FILE
  • egrep command is extended version of the grep command and provides the -e and -E options automatically by default.
  • PATTERN1,PATTERN2 and PATTERN3 are the search terms or patterns we want to match.
  • | character is used to implement OR conditions.

In the following example, we match one of “ferrari”,”opel”,”audi”.

$ grep -E "ferrari|opel|audi" cars.txt

Источник

Оцените статью
Adblock
detector