3 ms·
I don't want to be That Guy who says his way is best but I can only really see this being useful for someone who knows no shell tools at all. > ps -A | grep ru
by cranekam 5y ago
I don't want to be That Guy who says his way is best but I can only really see this being useful for someone who knows no shell tools at all.
> ps -A | grep ruby | grep fsevent | ruby-each-line "puts l.split.first" | xargs kill -9
killall -9 -r ruby.\*fsevent
> cat c | ruby-each-line "puts l.split.first[0..-2]" >> .env.development
awk '{ print(substr($1, 0, length($1) - 1)) }' c >> .env.development
or
awk '{ print $1 }' c | rev | cut -c2- | rev >> .env.development
> pbpaste | ruby-each-line "puts l.split.last" | ruby-all-lines "puts lines.map(&:strip).join(',')"
pbpaste | awk '{ print $NF }' | paste -sd,
(I couldn't work out exactly what the last two did so I'm not sure my translation is accurate. Even if it's not quite right I doubt the intent would be impossible to express with basic tools.)
- arp242 5y agoThat first pkill/killall example is a bit silly, but for the others I can see how a "ruby-each-line" can be useful, especially if you're already familiar with Ruby (although I'd probably name it "el", as that's much less typing). I never really learned awk properly myself either, in spite of being quite familiar with most other shell tools. "ruby-each-line" does more or less the same thing as awk. After all, there's just one person using your shell: you. So whatever works well for you is a "good" solution.
- cranekam 5y agoYeah, like I said: I can see how it could be useful for someone who knows only Ruby. > I never really learned awk properly myself either It's really worth it. It's simple — pattern matches and block, BEGIN/END, hashes, match() and gsub() cover 90% of my uses. I very often find myself writing something like this: awk -v id=$id ' $1 == id { go = 1 } { if (go && $4 == "b667226") { km += $5 secs += $6 n++ } } END { printf("%d rides, %d km (approx %.1f hours)\n", n, km / 1000, secs / 3600) }' This program takes STDIN, reads each line and: * sets a flag indicating interesting data has been found if the first column matches an ID passes on the command line * if the flag is set and column 4 contains "b667226", add columns 5 and 6 to running totals, and add 1 to a count of matching lines * after all the input has been read, prints out a summary of the data Of course, any language can do something like this, but awk is succinct, easy to iterate on, and available almost everywhere.
- arp242 5y agoawk just always seemed a little too domain-specific to really invest time in. The number of times I think "gee, I really wish I knew better awk" are few. This week I've been doing some Lua programming; I had done some Lua several years ago for something else, but found that I had forgotten most things. Even for Ruby I've forgotten quite a lot, yet for two years I programmed Ruby every day for a living. It's just that I haven't done much Ruby since, and when I did some Ruby several months ago I had to look up quite a lot of basic syntax things because I had roughly remembered how it worked but not enough to actually get stuff done in it. In general, I find that effective practical programming skills are a bit of a "use it or lose it" thing. I don't think I'll use awk enough to not "lose it", even though it's a fairly small language, and most problems it solves can also be solved in other ways.
- cranekam 5y agoFair enough. My job involved a lot of text crunching and simple ad-hoc analysis that suited awk well. “Use it or lose it” is certainly true.