Live data from Hacker News

Skip grep, use awk

blog.jpalardy.com

61–70 of 136 posts

Re: Skip grep, use awk

#61
post #52

AWK is the general-purpose programmatic filter and reporting tool in the Unix pipeline. Sed, grep and cut are specializations for specific use cases whose implementations might have better performance. Perl and Python are probably too general-purpose for writing compact one-liners in a pipeline.

Even though it's more characters, I usually use perl -pe 's/search/replace/' instead of sed in pipelines because it understands /n (and other escape characters I don't remember). Because all it takes is to get burned a couple times for it to be worth sticking with what you know will work.

The Perl version is considerably faster on large files as well. Or at least, it is when compared to the version of BSD sed shipped with OSX.

Re: Skip grep, use awk

#62
post #51

The author mentions that using awk instead of grep -v is not a good idea, what about: awk '! /something/' Doesn't it reproduce the same behaviour as the following? grep -v 'something'

Yup, was going to post this as a comment myself.

Re: Skip grep, use awk

#63
It's a nice idea, but I think maybe only to replace a simple egrep.

grep has tons of great option like

-F = fgrep; no regex - way faster

-v = as mentioned in the article

-o = print only matched input, not the entire line

-C = context, print lines before and after the match; can also be used partially with -A (after) and -B (before)

Re: Skip grep, use awk

#64

My favourite bad example of using grep was from a big enterprise software vendor to kill one of their processes. It looked something like ps -ef| grep SomeDaemon | grep -v grep | grep -v perl | perl -e ' '

Serious question: what would be a better way to do this?

  do_something_with_pids $(ps -C SomeDaemon ho pid)
Though it's GNU procps.

Re: Skip grep, use awk

#66
post #52

AWK is the general-purpose programmatic filter and reporting tool in the Unix pipeline. Sed, grep and cut are specializations for specific use cases whose implementations might have better performance. Perl and Python are probably too general-purpose for writing compact one-liners in a pipeline.

Even though it's more characters, I usually use perl -pe 's/search/replace/' instead of sed in pipelines because it understands /n (and other escape characters I don't remember). Because all it takes is to get burned a couple times for it to be worth sticking with what you know will work.

"/n"? What's that? My `man perlre' doesn't say anything about this regexp flag.

Re: Skip grep, use awk

#68
post #52

Earlier quoted context omitted.

Even though it's more characters, I usually use perl -pe 's/search/replace/' instead of sed in pipelines because it understands /n (and other escape characters I don't remember). Because all it takes is to get burned a couple times for it to be worth sticking with what you know will work.

The Perl version is considerably faster on large files as well. Or at least, it is when compared to the version of BSD sed shipped with OSX.

This is probably not the case with the sed in Linux.

Re: Skip grep, use awk

#69
post #66
post #52

Earlier quoted context omitted.

Even though it's more characters, I usually use perl -pe 's/search/replace/' instead of sed in pipelines because it understands /n (and other escape characters I don't remember). Because all it takes is to get burned a couple times for it to be worth sticking with what you know will work.

"/n"? What's that? My `man perlre' doesn't say anything about this regexp flag.

because OP is referring to escape characters I assume it is a typo for "\n"

Re: Skip grep, use awk

#70
> awk uses modern (read “Perl”) regular expressions, by default – like grep -E

No, Perl regular expression are different than extended regular expressions that awk and "grep -E" use.

Post reply on HN