5 ms·
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?
by waynesonfire 5y ago
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?
- xorcist 5y agoThat 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.
- danadam 5y agoWith sed instead of grep, and probably not much better, but in a single command: cat hello | sed -n '/foo/{ /bar/{ /baz/ p }}'
- ravoori 5y agowith awk: <hello awk '/foo/ && /bar/ && /baz/'
- BenjiWiebe 5y agoKeep 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.