News for hackers. Stuff that matters.
Re:Grep flags – the good stuff by waynesonfire on Wednesday February 02, 2022 @12:32PM (#30180827)
very frequently I want to chain grep things... I lean on the "|" operator for this, e.g. cat hello | grep foo | grep bar and it seems verbose. any tips?
Re:Grep flags – the good stuff by xorcist on Wednesday February 02, 2022 @01:00PM (#30181299)
That kind of matches are what regexps were intended for:

  grep "foo.*bar" hello
Unfortunately, the basic grep syntax doesn't give you an easy way of specifying both orders, so that would have to be something like:

  grep -e "foo.*bar" -e "bar.*foo" hello
You can specify random order in a couple of different ways with Perl-compatible regexes, such as lookaheading the search terms from the end of line marker. But it's not as easy to read as it should be.
Re:Grep flags – the good stuff by danadam on Wednesday February 02, 2022 @01:14PM (#30181522)
With sed instead of grep, and probably not much better, but in a single command:

  cat hello | sed -n '/foo/{ /bar/{ /baz/ p }}'
Re:Grep flags – the good stuff by ravoori on Wednesday February 02, 2022 @03:42PM (#30183751)
with awk: <hello awk '/foo/ && /bar/ && /baz/'
Re:Grep flags – the good stuff by BenjiWiebe on Wednesday February 02, 2022 @09:42PM (#30187816)
Keep doing it that way, I'd say. The other replies are ways to do it without chaining grep, but they don't seem any less verbose and definitely are less obvious.