Size: 478
Comment:
|
Size: 1006
Comment: added grep -e 'foo' -e 'bar'
|
Deletions are marked like this. | Additions are marked like this. |
Line 4: | Line 4: |
Well, for lines containing foo AND bar, two grep statements are needed. | The easiest way to match both foo AND bar is to use two {{{grep}}} commands: |
Line 7: | Line 7: |
grep foo| grep bar | grep foo | grep bar |
Line 10: | Line 10: |
If you prefer, you can achieve this in one sed, or awk statement. | It can also be done with one {{{egrep}}}, although (as you can probably guess) this doesn't really scale well to more than two patterns: {{{ egrep 'foo.*bar|bar.*foo' }}} If you prefer, you can achieve this in one {{{sed}}} or {{{awk}}} statement. (The {{{awk}}} example is probably the most scalable.) |
Line 17: | Line 23: |
And for lines containing foo OR bar, grep can do it "nicely", but it can also be done with sed, awk, etc. | To match lines containing foo OR bar, {{{egrep}}} is the natural choice, but it can also be done with {{{sed}}}, {{{awk}}}, etc. |
Line 21: | Line 27: |
grep -E 'foo|bar' | # some people prefer grep -E 'foo|bar' # This is another option, some people prefer: grep -e 'foo' -e 'bar' |
Line 23: | Line 32: |
{{{egrep}}} is the oldest and most portable form of the {{{grep}}} command using Extended Regular Expressions (EREs). {{{-E}}} is a POSIX-required switch. |
How can I grep for lines containing foo AND bar, foo OR bar?
The easiest way to match both foo AND bar is to use two grep commands:
grep foo | grep bar
It can also be done with one egrep, although (as you can probably guess) this doesn't really scale well to more than two patterns:
egrep 'foo.*bar|bar.*foo'
If you prefer, you can achieve this in one sed or awk statement. (The awk example is probably the most scalable.)
sed -n '/foo/{/bar/p}' awk '/foo/ && /bar/'
To match lines containing foo OR bar, egrep is the natural choice, but it can also be done with sed, awk, etc.
egrep 'foo|bar' # some people prefer grep -E 'foo|bar' # This is another option, some people prefer: grep -e 'foo' -e 'bar'
egrep is the oldest and most portable form of the grep command using Extended Regular Expressions (EREs). -E is a POSIX-required switch.