Live data from Hacker News

Skip grep, use awk

blog.jpalardy.com

51–60 of 136 posts

Re: Skip grep, use awk

#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'

Re: Skip grep, use awk

#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.

Re: Skip grep, use awk

#53

Earlier quoted context omitted.

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

As per other users, you can use pkill but it's not entirely portable in meaning between OSs. This software had to run on just about every possible Unix out there. Solaris, HP-UX, AIX, many others ... My main point is that using perl and grep, (multiple thereof!) is nuts when you can do it all in perl. Also crazy was using -ef (all processes) when the process of interest was using a known user. So ps -u would be more…

If it's something you need to do on a semi-regular basis, using a pidfile seems to me to be the best solution. Instead of trying to be clever about pipelines, just write the pid(s) to a file and use that.

Re: Skip grep, use awk

#54
Except "grep | awk" is often way quicker because it does not need to split the record before it does anything and I usually want to span more processes because awk will usually eat a whole cpu if it's doing something gnarly.

Re: Skip grep, use awk

#55
post #45

Earlier quoted context omitted.

That's what I was referencing when I said > Pulling out a field from a line is what most people use awk for, but it's honestly the least interesting part of awk. In fact, if cut supported regular expressions for specifying the field and record separators people wouldn't be using awk for that purpose (because that's all that $n does). There's nothing magical about awk's default FS. It's literally just /\s+/. If cut's…

The default FS throws away leading blanks, though, which doesn't happen if you set it explicitly to \s+, so a tiny little bit of magic does go on after all.

Fair enough, though ultimately you could emulate it with sed:

    some-cmd | sed -E '{ s/^\s+//g ; s/\s+/ /g }' | cut -f$n

Re: Skip grep, use awk

#56
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'

Yes, these are the same

Re: Skip grep, use awk

#57

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?

A pid file is the best IMO

Re: Skip grep, use awk

#60
post #20
post #16

Earlier quoted context omitted.

grep -v grep.. I wish I could admit to using this less often than I do; but it's always manual not automated. At some point I'll figure out a better alternative than this or pgrep which often misses processes.

Just do 'grep [S]omeDaemon', and you won't see your grep line.

No, please don't do that. Use filtering capabilities of ps, so grep becomes completely unnecessary.
Post reply on HN