Skip to main content
Version: v1.14.0

grep

Function

grep search strings inside of values

Synopsis

grep(<pattern> [, e: any]) -> bool

Description

The grep function searches all of the strings in its input value e (or this if e is not given) using the <pattern> argument, which can be a regular expression, glob pattern, or string. If the pattern matches for any string, then the result is true. Otherwise, it is false.

Note that string matches are case insensitive while regular expression and glob matches are case sensitive. In a forthcoming release, case sensitivity will be a expressible for all three pattern types.

The entire input value is traversed:

  • for records, each field name is traversed and each field value is traversed or descended if a complex type,
  • for arrays and sets, each element is traversed or descended if a complex type, and
  • for maps, each key and value is traversed or descended if a complex type.

Examples

Reach into nested records

echo '{foo:10}{bar:{s:"baz"}}' | zq -z 'grep("baz")' -

=>

{bar:{s:"baz"}}

It only matches string fields

echo '{foo:10}{bar:{s:"baz"}}' | zq -z 'grep("10")' -

=>


Match a field name

echo '{foo:10}{bar:{s:"baz"}}' | zq -z 'grep("foo")' -

=>

{foo:10}

Regular expression

echo '{foo:10}{bar:{s:"baz"}}' | zq -z 'grep(/foo|baz/)' -

=>

{foo:10}
{bar:{s:"baz"}}

Glob with a second argument

echo '{s:"bar"}{s:"foo"}{s:"baz"}{t:"baz"}' | zq -z 'grep(b*, s)' -

=>

{s:"bar"}
{s:"baz"}