3 ms·
Yeah, 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.
by cranekam 5y ago
Yeah, 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.