Live data from Hacker News

Grep flags – the good stuff

zwischenzugs.com

11–20 of 53 posts

Re: Grep flags – the good stuff

#11
I think I have never used grep -r. I'm sure gnu grep has some way to specify which files to search, but why would I learn that syntax as well when I already know find, and exec works (exec + is much faster, but exec ; gets you the results too if your find lacks exec +).

Re: Grep flags – the good stuff

#13
My favorite feature is:

       -P, --perl-regexp
       
              Interpret I as Perl-compatible regular
              expressions (PCREs). This option is experimental when
              combined with the -z (--null-data) option, and grep -P
              may warn of unimplemented features.
As everything (python, Go, javascript, etc, etc) uses perl regexps now-a-days and I can never remember which things I need to escape for old gods regexp.

Re: Grep flags – the good stuff

#15

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?

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

#16
post #11

I think I have never used grep -r. I'm sure gnu grep has some way to specify which files to search, but why would I learn that syntax as well when I already know find, and exec works (exec + is much faster, but exec ; gets you the results too if your find lacks exec +).

Check out ripgrep

Re: Grep flags – the good stuff

#17
Learning about -o has decreased my use of sed considerably. Where I used to use:

    sed -n 's/.*\(pattern\).*/\1/p'
it can instead simply be:

    grep -o 'pattern'
The -w flag is new to me today - excited to save still more keystrokes!

Re: Grep flags – the good stuff

#18
post #13

My favorite feature is: -P, --perl-regexp Interpret I as Perl-compatible regular expressions (PCREs). This option is experimental when combined with the -z (--null-data) option, and grep -P may warn of unimplemented features. As everything (python, Go, javascript, etc, etc) uses perl regexps now-a-days and I can never remember which things I need to escape for old gods regexp.

Haha, agreed 100%. My go to for years had been https://www.regexplanet.com/. It let's you test what regex escapes work where without spinning up one-off `void main()s`.

Re: Grep flags – the good stuff

#19
post #11

I think I have never used grep -r. I'm sure gnu grep has some way to specify which files to search, but why would I learn that syntax as well when I already know find, and exec works (exec + is much faster, but exec ; gets you the results too if your find lacks exec +).

Check out ripgrep

...you beat me to it! RipGrep[0] is among my favorite semi-recently discovered CLI tools.

[0] - https://github.com/BurntSushi/ripgrep#why-should-i-use-ripgr...

Re: Grep flags – the good stuff

#20

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?

With sed instead of grep, and probably not much better, but in a single command:

  cat hello | sed -n '/foo/{ /bar/{ /baz/ p }}'
Post reply on HN