News for hackers. Stuff that matters.
Re:Awk in 20 Minutes (2015) by j_z_reeves on Saturday May 02, 2020 @03:41AM (#23050120)
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 correct however...

Re:Awk in 20 Minutes (2015) by tejtm on Saturday May 02, 2020 @01:40PM (#23053467)
j_z_reeves 9 hours ago [-]

> cat logfile | awk '/ERROR:/ {counts[$1] = counts[$1] + 1}; END { for (day in counts) print day " : " counts[day]}' | sort

Great first program! a bit less verbose could be

> awk '/ERROR:/ {counts[$1]++}END{...}' logfile

there are also ways of sorting the output but within (g)awk (asort & asorti) but sorting externally as you have is more flexible and engages another core which can be faster on large input

Re:Awk in 20 Minutes (2015) by j0057 on Saturday May 02, 2020 @05:46AM (#23050618)
Without knowing the format of logfile, that still seems obviously correct to me.
Re:Awk in 20 Minutes (2015) by j_z_reeves on Saturday May 02, 2020 @12:38PM (#23052886)
I was surprised by the counts[$1] = counts[$1] + 1, since I didn't think it would correctly coerce a non-existing value to a 0.
Re:Awk in 20 Minutes (2015) by xorcist on Saturday May 02, 2020 @08:44AM (#23051428)
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

Re:Awk in 20 Minutes (2015) by j0057 on Sunday May 03, 2020 @10:47AM (#23059310)
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) by xorcist on Saturday May 09, 2020 @04:21AM (#23123079)
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) by j_z_reeves on Saturday May 02, 2020 @12:36PM (#23052863)
yes that would also work! I forgot about the `-c` argument for uniq.