Last Updated: February 25, 2016
·
3.085K
· sheerun

Ruby -ane as awk replacement

Don't you think the awk syntax is a bit awkward?

ps | grep xxx | awk '{ print $1 }' | xargs kill

I provide you an easy to remember ruby equivalent:

ps | grep xxx | ruby -ane 'puts $F[0]' | xargs kill

Although slightly longer, you can put any valid ruby code in quotes and it'll just work! The 'a' flag splits each line into $F array, and the 'n' flag assumes 'while gets(); ... end' loop around code provided by 'e'.

@edit:

Konrad thinks awk is better tool for such things. The thing is: I hate perl and awk, I love Ruby. Even if it may execute slightly slower, I feel comfortable with that. Of course I don't recommend it if you care about portability or you're writing an open source script.

1 Response
Add your response

@telemachus wow, that's pretty good article :) thank you for the comment!

over 1 year ago ·