laarcnew | comments | discord | tags | ask | show | place | submitlogin
Unexpected Interaction Of Features (solipsys.co.uk)
5 points by RiderOfGiraffes to programming linux software 182 days ago | 3 comments


2 points by rain1 182 days ago

This is a nice example of the unix philosophy. Showing how important orthogonality is in programming.

-----

2 points by akkartik 182 days ago

Indeed: http://harmful.cat-v.org/cat-v/unix_prog_design.pdf

-----


I prefer awk for this. Given the input

  5 which 
  15 aerodynamically 
  20 electroencephalogram 
  10 exotically 
  15 aerodynamically 
  1 a 
  15 differentiation
then

  $ pbpaste | awk '{ _[$1][$2]++; } END { for (k in _) { for (v in _[k]) { print k, v; } } }'
  1 a
  5 which
  10 exotically
  15 aerodynamically
  15 differentiation
  20 electroencephalogram
It's easy to print out counts too, if you want.

  $ pbpaste | awk '{ _[$1][$2]++; } END { for (k in _) { for (v in _[k]) { print _[k][v], "", k, v; } } }'
  1  1 a
  1  5 which
  1  10 exotically
  2  15 aerodynamically
  1  15 differentiation
  1  20 electroencephalogram
Obviously, it's way simpler to use `pbpaste | sort -n | uniq -c`. But it was useful to spend the time to learn awk. Performance, for example. I was able to generate https://www.laarc.io/traffic-all.png using the above techniques. (That's a rolling 24 hour average of laarc's view count / unique IPs, which is a lot more precise than simple daily sums: https://www.laarc.io/traffic-2019-02-17.png)

Awk also gracefully ignores leading whitespace of varying lengths.

Ref: https://stackoverflow.com/a/1916188

-----




Welcome | Guidelines | Bookmarklet | Feature Requests | Source | Contact | Twitter | Lists

RSS (stories) | RSS (comments)

Search: