Differences between revisions 15 and 37 (spanning 22 versions)
Revision 15 as of 2010-07-22 16:26:40
Size: 3248
Editor: GreyCat
Comment: break into 3 parts for easier reading
Revision 37 as of 2023-01-26 22:54:33
Size: 4194
Editor: emanuele6
Comment: sed before awk
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
== How can I grep for lines containing foo AND bar, foo OR bar? Or for files containing foo AND bar, possibly on separate lines? ==
This is really three different questions, so we'll break this answer into three parts.
== How can I grep for lines containing foo AND bar, foo OR bar? Or for files containing foo AND bar, possibly on separate lines?  Or files containing foo but NOT bar? ==
This is really four different questions, so we'll break this answer into parts.
Line 9: Line 9:
{{{ {{{#!highlight bash
Line 11: Line 11:
grep foo "$myfile" | grep bar # for those who need the hand-holding grep foo -- "$myfile" | grep bar # for those who need the hand-holding
Line 14: Line 14:
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: It can also be done with one {{{grep}}}, although (as you can probably guess) this doesn't really scale well to more than two patterns:
Line 16: Line 16:
{{{
egrep 'foo.*bar|bar.*foo'
{{{#!highlight bash
grep -E 'foo.*bar|bar.*foo'
Line 22: Line 22:
{{{
sed -n '/foo/{/bar/p}'
{{{#!highlight bash
sed '/foo/!d; /bar/!d'
Line 27: Line 27:
If you need to scale the awk solution to an arbitrary number of patterns, you can construct the awk command on the fly: If you need to scale the awk solution to an arbitrary number of patterns, you can write a function like this:
Line 29: Line 29:
{{{
# bash, ksh93
# Constructs awk "/$1/&&/$2/&&...."
# Data to be matched should be on stdin.
# Writes matching lines to stdout.
multimatch() {
  (($# < 2)) && { echo "usage: multimatch pat1 pat2 [...]" >&2; return 1; }
  awk "/$1/$(printf "&&/%s/" "${@:2}")"
{{{#!highlight sh
# POSIX
multimatch() { # usage: multimatch pattern...
  awk '
    BEGIN {
      for ( i = 1; i < ARGC; i++ )
        a[i] = ARGV[i]
      ARGC = 1
    }
    {
      for (i in a)
        if ($0 !~ a[i])
          next
      print
    }' "$@"
Line 44: Line 51:
{{{ {{{#!highlight bash
Line 48: Line 55:
Or you can construct one pattern with {{{egrep}}} (or `grep -E`): Or you can separate the patterns with newlines:
Line 50: Line 57:
{{{
egrep 'foo|bar'
{{{#!highlight bash
grep 'foo
bar'
}}}

Or you can construct one pattern with {{{grep -E}}}:

{{{#!highlight bash
Line 59: Line 72:
{{{ {{{#!highlight bash
sed -n -e '/foo/{ p; d; }' -e '/bar/{ p; d; }'
Line 67: Line 81:
{{{ {{{#!highlight bash
Line 69: Line 83:
# some people prefer egrep -v 'foo|bar' }}}

Or using {{{sed}}}, or {{{awk}}}:
{{{#!highlight bash
sed -e '/foo/d' -e '/bar/d'
awk '!/foo|bar/'
Line 76: Line 95:
{{{
grep -q foo "$myfile" && grep -q bar "$myfile" && echo "Found both"
{{{#!highligh bash
if grep -q foo "$myfile" && grep -q bar "$myfile"; then
printf 'Found both\n'
fi
Line 83: Line 104:
{{{
awk '/foo/{a=1} /bar/{b=1} a&&b{print "both found";exit} END{if (a&&b){ exit 0} else{exit 1}}'
{{{#!highligh bash
if awk '/foo/{a=1} /bar/{b=1} a&&b{exit} END{if(a&&b){exit 0};exit 1}' "$myfile"; then
  printf 'Found both\n'
fi
Line 89: Line 112:

A perl one-liner that scales to any number of patterns, while also reading each input file only once:

{{{#!highligh bash
perl -e '@pat=("foo","bar"); local $/; L: for $f (@ARGV){open(FH,,$f); $a=<FH>; for(@pat){next L unless $a =~ $_} print "$f\n"}'
}}}

=== foo but NOT bar in the same file, possibly on different lines ===

This is a variant of the previous case. The advantage here is that if we find "bar", we can stop reading. Here's an awk solution:

{{{#!highligh bash
awk '/foo/{good=1} /bar/{good=0;exit} END{exit !good}'
}}}

How can I grep for lines containing foo AND bar, foo OR bar? Or for files containing foo AND bar, possibly on separate lines? Or files containing foo but NOT bar?

This is really four different questions, so we'll break this answer into parts.

foo AND bar on the same line

The easiest way to match lines that contain both foo AND bar is to use two grep commands:

   1 grep foo | grep bar
   2 grep foo -- "$myfile" | grep bar   # for those who need the hand-holding

It can also be done with one grep, although (as you can probably guess) this doesn't really scale well to more than two patterns:

   1 grep -E 'foo.*bar|bar.*foo'

If you prefer, you can achieve this in one sed or awk statement:

   1 sed '/foo/!d; /bar/!d'
   2 awk '/foo/ && /bar/'

If you need to scale the awk solution to an arbitrary number of patterns, you can write a function like this:

   1 # POSIX
   2 multimatch() { # usage: multimatch pattern...
   3   awk '
   4     BEGIN {
   5       for ( i = 1; i < ARGC; i++ )
   6         a[i] = ARGV[i]
   7       ARGC = 1
   8     }
   9     {
  10       for (i in a)
  11         if ($0 !~ a[i])
  12           next
  13       print
  14     }' "$@"
  15 }

foo OR bar on the same line

There are lots of ways to match lines containing foo OR bar. grep can be given multiple patterns with -e:

   1 grep -e 'foo' -e 'bar'

Or you can separate the patterns with newlines:

   1 grep 'foo
   2 bar'

Or you can construct one pattern with grep -E:

   1 grep -E 'foo|bar'

(You can't use the | union operator with plain grep. | is only available in Extended Regular Expressions.)

It can also be done with sed, awk, etc.

   1 sed -n -e '/foo/{ p; d; }' -e '/bar/{ p; d; }'
   2 awk '/foo|bar/'

The awk approach has the advantage of letting you use awk's other features on the matched lines, such as extracting only certain fields.

To match lines that do not contain "foo" AND do not contain "bar":

   1 grep -E -v 'foo|bar'

Or using sed, or awk:

   1 sed -e '/foo/d' -e '/bar/d'
   2 awk '!/foo|bar/'

foo AND bar in the same file, not necessarily on the same line

If you want to match files (rather than lines) that contain both "foo" and "bar", there are several possible approaches. The simplest (although not necessarily the most efficient) is to read the file twice:

if grep -q foo "$myfile" && grep -q bar "$myfile"; then
  printf 'Found both\n'
fi

The double grep -q solution has the advantage of stopping each read whenever it finds a match; so if you have a huge file, but the matched words are both near the top, it will only read the first part of the file. Unfortunately, if the matches are near the bottom (worst case: very last line of the file), you may read the whole file two times.

Another approach is to read the file once, keeping track of what you've seen as you go along. In awk:

if awk '/foo/{a=1} /bar/{b=1} a&&b{exit} END{if(a&&b){exit 0};exit 1}' "$myfile"; then
  printf 'Found both\n'
fi

It reads the file one time, stopping when both patterns have been matched. No matter what happens, the END block is then executed, and the exit status is set accordingly.

If you want to do additional checking of the file's contents, this awk solution can be adapted quite easily.

A perl one-liner that scales to any number of patterns, while also reading each input file only once:

perl -e '@pat=("foo","bar"); local $/; L: for $f (@ARGV){open(FH,,$f); $a=<FH>; for(@pat){next L unless $a =~ $_} print "$f\n"}'

foo but NOT bar in the same file, possibly on different lines

This is a variant of the previous case. The advantage here is that if we find "bar", we can stop reading. Here's an awk solution:

awk '/foo/{good=1} /bar/{good=0;exit} END{exit !good}'

BashFAQ/079 (last edited 2023-01-26 22:54:33 by emanuele6)