Live data from Hacker News

Don’t underestimate grep-based code scanning

littlemaninmyhead.wordpress.com

71–80 of 122 posts

Re: Don’t underestimate grep-based code scanning

#71
post #3

One thing which was not immediately obvious to me for a while: the stricter your language’s formatting is, the easier it will be to grep source code. I work a lot with Go, where all code in our repository is gofmt'ed. You can get quite far with regular expressions for finding/analyzing Go code. (And when regexps don’t cut it anymore, Go has excellent infrastructure for working with it programmatically. http://golang.…

Another thing that's not immediately obvious is the longer your search string is in grep, the faster it will find your results.

Re: Don’t underestimate grep-based code scanning

#72
The ability to easily grep for functions in C-like code is why I've come to appreciate projects defining their functions like:

  int
  foo_func(void) {
You can grep for `^foo_func\b` to get to a declaration or definition, or `^foo_func\b.* {$` to get to a definition or `^foo_func\b.* ;` to get to a declaration. This is instead of using something like `^\w.* \bfoo_func\(`, which is what you'd need for:

  int foo_func(void) {
By the way, anyone know of a way to insert a literal asterisk here without having to follow it up with a space?

Re: Don’t underestimate grep-based code scanning

#73
post #52

Don't use grep. Use ag[0], which is specifically designed for searching code. It's much faster, honors .gitignore, and the output can be piped back through grep if you like. ag FooBar | grep -v Baz It's in brew/apt/yum etc as `the_silver_searcher` (although brew install ag works fine too). 0: https://github.com/ggreer/the_silver_searcher

why not use a posix standard tool which is available everywhere? I appreciate the suggestion, but its worded a little strongly.

Re: Don’t underestimate grep-based code scanning

#74
post #52

Don't use grep. Use ag[0], which is specifically designed for searching code. It's much faster, honors .gitignore, and the output can be piped back through grep if you like. ag FooBar | grep -v Baz It's in brew/apt/yum etc as `the_silver_searcher` (although brew install ag works fine too). 0: https://github.com/ggreer/the_silver_searcher

+1 Here's why:

ag provides sane default and settings for developers. grep is ubiquitous and great, but to do what most developers want it requires some guidance, whereas ag focuses on being what you want most of the time.

What I mean by that is that I enjoy the smart-case sensitivity (as in, if there are not caps in my pattern, then it defaults to case-insensitive but if I have any caps in my pattern, it uses case-sensitive) or fast filename searching with -g or both with -G.

I've tried rg and while it was faster, it also didn't provide as good support for filename searching. Ag is still what I consider "so fast I almost don't believe it"

It's like the 'tldr' command (https://tldr.sh/). Of course I still use man pages, but having something that gets me what I really need very quickly is important.

Steps:

1. Use ag

2. If ag isn't present, try to install ag

3. If I can't install ag, then use grep or find, no big deal.

ag > ack > grep/find

Re: Don’t underestimate grep-based code scanning

#75
post #69
post #52

Don't use grep. Use ag[0], which is specifically designed for searching code. It's much faster, honors .gitignore, and the output can be piped back through grep if you like. ag FooBar | grep -v Baz It's in brew/apt/yum etc as `the_silver_searcher` (although brew install ag works fine too). 0: https://github.com/ggreer/the_silver_searcher

Is there a reason to use ag rather than rg? afair the latter was a lot faster when I tried it (on ubuntu / intel).

Same experience here, started with ack and switched to ag and then to rg for speed. I've found them roughly equivalent in functionality, but for those who need specific features here's a link to a feature comparison table:

https://beyondgrep.com/feature-comparison/

Re: Don’t underestimate grep-based code scanning

#77
post #69
post #52

Don't use grep. Use ag[0], which is specifically designed for searching code. It's much faster, honors .gitignore, and the output can be piped back through grep if you like. ag FooBar | grep -v Baz It's in brew/apt/yum etc as `the_silver_searcher` (although brew install ag works fine too). 0: https://github.com/ggreer/the_silver_searcher

Is there a reason to use ag rather than rg? afair the latter was a lot faster when I tried it (on ubuntu / intel).

Pretty sure that rg is a successor to ag, so most people who use `ag` are just ones who haven't heard of `rg` yet.

Well, there's no true hierarchy/succession order but rg was written after ag was.

Re: Don’t underestimate grep-based code scanning

#78
post #75
post #69

Earlier quoted context omitted.

Is there a reason to use ag rather than rg? afair the latter was a lot faster when I tried it (on ubuntu / intel).

Same experience here, started with ack and switched to ag and then to rg for speed. I've found them roughly equivalent in functionality, but for those who need specific features here's a link to a feature comparison table: https://beyondgrep.com/feature-comparison/

That table is quite out of date for ripgrep, which has added a number of features. See: https://github.com/beyondgrep/website/issues/97

Re: Don’t underestimate grep-based code scanning

#79
post #69

Earlier quoted context omitted.

Is there a reason to use ag rather than rg? afair the latter was a lot faster when I tried it (on ubuntu / intel).

Pretty sure that rg is a successor to ag, so most people who use `ag` are just ones who haven't heard of `rg` yet. Well, there's no true hierarchy/succession order but rg was written after ag was.

Yes, this is true, although ripgrep is more of a hybrid than ag is. ag has numerous problems with being treated as a normal `grep` tool, where as ripgrep does not. (Although, to be clear, ripgrep is not POSIX compatible.)

Re: Don’t underestimate grep-based code scanning

#80
post #72

The ability to easily grep for functions in C-like code is why I've come to appreciate projects defining their functions like: int foo_func(void) { You can grep for `^foo_func\b` to get to a declaration or definition, or `^foo_func\b.* {$` to get to a definition or `^foo_func\b.* ;` to get to a declaration. This is instead of using something like `^\w.* \bfoo_func\(`, which is what you'd need for: int foo_func(void)…

Yes, I've been doing this since I saw it in the BSD source back in the '80s.
Post reply on HN