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.
Written by Adam Stankiewicz
Related protips
1 Response
@telemachus wow, that's pretty good article :) thank you for the comment!
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#