Hacker News new | ask | show | jobs
by ploxiln 3335 days ago
Yeah, the "ps | grep | grep -v grep" pattern is silly, there are a few better ways. In this case just:

    old_pids=$(pgrep '^haproxy')
2 comments

"... is silly, there are better ways."

I see this usage often where it seems like

  grep pattern1 | grep -v pattern2 
can be replaced by

  sed -n '/pattern1/d;/pattern2/p'
or at least

  sed '/pattern1/!d' | sed '/pattern2/d'
or

  sed -n 's/pattern1pattern2//g;/pattern2/p'
But I must be missing something obvious.

For example look at the "grep -v" usage here:

https://github.com/thomwiggers/qhasm/raw/master/qhasm-arm

Is there something wrong with using

   sed '/^op:livefloat80:/d'
Moreover, in the last line, why not use

  sed 's/\$/#/g'
instead of

  tr '$' '#'
Apologies if I am missing the obvious.
All of your suggestions are more complicated than what you're suggesting replacing. People chain simple commands together because they're a language and it matches how they think of the problem. They're solving the problem with simple commands and pipes, you're trying to solve it with regex and as few commands as possible. All ways are valid but specific commands tend to be easier to remember on the fly than trying to do it all with sed and regexes. I use sed when I want to edit streams, not when I want to filter them. Tr is a simpler replace than a sed regex.

    grep [h]aproxy
It's not silly at all, it's simple; there's many ways to accomplish something and knowing a shorter more precise way to do something doesn't make the longer simple ways silly. The author didn't know about pgrep, so he used what he did know about, ps and grep, nothing remotely silly about that; it's pragmatic.