Live data from Hacker News

Awk in 20 Minutes (2015)

ferd.ca

121–128 of 128 posts

Re: Awk in 20 Minutes (2015)

#121
post #74

nice, I finally took the time to read the man pages for awk. And whipped out a script to count the number of errors occurred for a particular day for a postgres log file. cat logfile | awk '/ERROR:/ {counts[$1] = counts[$1] + 1}; END { for (day in counts) print day " : " counts[day]}' | sort I just needed to know how awk programs are structured, the rest is just simple programming! EDIT: I'm not sure if it's actually…

Apart from the useless use of cat, since sort does the work here something like the following would probably suffice: grep ERROR logfile | cut -f 1 -d ' ' | sort | uniq -c

There's really nothing useless about that use of cat: it makes the pipeline compose better from left to right. It's not like you have to pay 25 cents for each process you spawn.

Re: Awk in 20 Minutes (2015)

#123
post #116

One of the things I like to do with mawk.exe under Windows is to automate the same command over a group of files. Let's say the myPgm only takes one file name as a command line parameter, then I can so something like this: dir *.xyz /s/b | mawk "{print 'myPgm -x '$0}" | cmd If the file paths have spaces in them, then you have to wrap the name within double quotes. I have found it challenging to output those in mawk -…

Why not use good ol' for?

F.e for %f in (.doc .txt) do type %f

Re: Awk in 20 Minutes (2015)

#125

My absolute favorite example of what's possible with awk is this[0] calculator from Ward Cunningham about splitting expenses on a ski trip. It's a really beautiful little piece of code well-adapted to this problem. [0] - https://c2.com/doc/expense/

This reminds me of a story of a guy who wrote a whole company internal debit system like this (coffee, meals out, etc) and it turned into a currency. I feel like I either saw it here, or a similar forum. Anyone have a link? My searches have not found it...

This one? https://royrapoport.blogspot.com/2011/05/coffee-and-its-effe...

Re: Awk in 20 Minutes (2015)

#126
post #116

One of the things I like to do with mawk.exe under Windows is to automate the same command over a group of files. Let's say the myPgm only takes one file name as a command line parameter, then I can so something like this: dir *.xyz /s/b | mawk "{print 'myPgm -x '$0}" | cmd If the file paths have spaces in them, then you have to wrap the name within double quotes. I have found it challenging to output those in mawk -…

Why not use good ol' for? F.e for %f in ( .doc .txt) do type %f

I also need to do things like:

    docker container ls -a -q | mawk "{print 'docker rm '$1}" | cmd

Re: Awk in 20 Minutes (2015)

#127
post #121
post #74

Earlier quoted context omitted.

Apart from the useless use of cat, since sort does the work here something like the following would probably suffice: grep ERROR logfile | cut -f 1 -d ' ' | sort | uniq -c

There's really nothing useless about that use of cat: it makes the pipeline compose better from left to right. It's not like you have to pay 25 cents for each process you spawn.

So does the pipeline above.

It's not detrimental to performance since an empty cat is a no-op in a pipeline. You can have any number of them. But commands should be written for humans to understand, and inserting no-ops is a distraction to the reader.

In the trivial example, "grep needle haystack" reads better than "cat haystack | grep needle".

Re: Awk in 20 Minutes (2015)

#128

Earlier quoted context omitted.

This reminds me of a story of a guy who wrote a whole company internal debit system like this (coffee, meals out, etc) and it turned into a currency. I feel like I either saw it here, or a similar forum. Anyone have a link? My searches have not found it...

This one? https://royrapoport.blogspot.com/2011/05/coffee-and-its-effe...

That's it! You found it! Thank you.
Post reply on HN