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 ✵
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
grep("baz", this)
{foo:10}
{bar:{s:"baz"}}
Loading...
echo '{foo:10}
{bar:{s:"baz"}}' \
| super -s -c 'grep("baz", this)' -
It only matches string fields
grep("10", this)
{foo:10}
{bar:{s:"baz"}}
Loading...
echo '{foo:10}
{bar:{s:"baz"}}' \
| super -s -c 'grep("10", this)' -
Match a field name
grep("foo", this)
{foo:10}
{bar:{s:"baz"}}
Loading...
echo '{foo:10}
{bar:{s:"baz"}}' \
| super -s -c 'grep("foo", this)' -
Regular expression
grep('foo|baz', this)
{foo:10}
{bar:{s:"baz"}}
Loading...
echo '{foo:10}
{bar:{s:"baz"}}' \
| super -s -c 'grep('foo|baz', this)' -
Glob with a second argument
grep('^b.*', s)
{s:"bar"}
{s:"foo"}
{s:"baz"}
{t:"baz"}
Loading...
echo '{s:"bar"}
{s:"foo"}
{s:"baz"}
{t:"baz"}' \
| super -s -c 'grep('^b.*', s)' -