Live data from Hacker News

Use ack instead of grep to parse text files

stevengharms.com

31–40 of 51 posts

Re: Use ack instead of grep to parse text files

#31
post #16

Is it worth learning grep or ack or a similar tool? When I need to do these sort of tasks, I do them in a scripting language with some combination of split() and regex instead of using command line tools. But, I'm just doing that because it's what I know. Would I end up saving a significant amount of time if I learned to use grep instead?

I'd say start with ack for its ease of use, but knowing grep is important. I prefer ack but most of the boxes i work on don't have ack installed (and won't).

Re: Use ack instead of grep to parse text files

#32
post #16

Is it worth learning grep or ack or a similar tool? When I need to do these sort of tasks, I do them in a scripting language with some combination of split() and regex instead of using command line tools. But, I'm just doing that because it's what I know. Would I end up saving a significant amount of time if I learned to use grep instead?

I'd say start with ack for its ease of use, but knowing grep is important. I prefer ack but most of the boxes i work on don't have ack installed (and won't).

Also, these aren't particularly good examples of ack vs grep... as others have pointed out.

Re: Use ack instead of grep to parse text files

#33
post #2

ack '(?=silver).*needle' haystack is really not the same as: grep needle haystack|grep silver Because with the double grep method, a match will be made whether "silver" is before or after "needle"; while with the single ack command shown above, a match will only be made when "silver" comes before "needle". Also, I'm not sure why the author uses ack '(?=silver).*needle' haystack instead of simply: ack 'silver.*needle'…

ack hilights results by default, so `(?=silver.*)needle` would potentially be useful. But, yes, not the same as the grep|grep.

Re: Use ack instead of grep to parse text files

#34

Why is: ack -C5 'scope(?!.*lambda)' app/models Better than: grep -C5 scope app/models | grep -v lambda ?

The first one gets you five lines around all of the scopes without lambdas. The second one gets you five lines around all the scopes (including those with lambdas) and then omits all of the lines with lambdas, some of which will have caused a match in the first grep, and some of which may be part of the context of matches in the first grep. You will get both contexts with no match in the middle and matches with incomplete contexts.

Re: Use ack instead of grep to parse text files

#38

Why is: ack -C5 'scope(?!.*lambda)' app/models Better than: grep -C5 scope app/models | grep -v lambda ?

The first one gets you five lines around all of the scopes without lambdas. The second one gets you five lines around all the scopes (including those with lambdas) and then omits all of the lines with lambdas, some of which will have caused a match in the first grep, and some of which may be part of the context of matches in the first grep. You will get both contexts with no match in the middle and matches with incom…

Quite right. The downfalls of posting untested code...

Re: Use ack instead of grep to parse text files

#40
post #5
post #2

ack '(?=silver).*needle' haystack is really not the same as: grep needle haystack|grep silver Because with the double grep method, a match will be made whether "silver" is before or after "needle"; while with the single ack command shown above, a match will only be made when "silver" comes before "needle". Also, I'm not sure why the author uses ack '(?=silver).*needle' haystack instead of simply: ack 'silver.*needle'…

Maybe it's because you can write the latter also with plain grep: grep 'silver.*needle' haystack

That assumes that "silver" will appear before "needle", which may not always be the case. `grep needle file | grep silver` gets you lines containing both "silver" and "needle" but not necessarily in that order.
Post reply on HN