Live data from Hacker News

Useful Unix commands for data science

gregreda.com

41–50 of 108 posts

Re: Useful Unix commands for data science

#41
post #3

I like slicing and dicing with awk, grep and friends too. One thing I find odd that you have to drop to a full language (awk, perl etc) to sum a column of numbers. Am I missing a utility? echo "1\n2\n3\n" | sum # should print 6 with hyphothetical sum command I suppose more generally you could have a 'fold initial op' and: echo "1\n2\n3\n4\n" | fold 0 + # should print 10 echo "1\n2\n3\n4\n" | fold 1 \* # should print…

    echo "1\n2\n3\n" | tr '\n' + | bc

Re: Useful Unix commands for data science

#43
post #22

Earlier quoted context omitted.

Could you expand on why export LC_ALL=C would "make all your commands 3x faster"?

Gnu grep is or was very slow with the UTF-8 locale. Not sure about other commands, perhaps anything that processes text, awk and sed maybe?

That was mostly fixed. http://savannah.gnu.org/bugs/?14472

Re: Useful Unix commands for data science

#44
post #29
post #11

Starts off with unnecessary use of cat, e.g., cat file | awk 'cmds'. One can simply do awk 'cmds' file.

I know purists always complain about unnecessary cats, but I always find it useful to start with "head" or "tail" in the first position to figure out my pipeline, and then replace it with cat when it's all working. And if the extra cat is actually making a measurable difference, maybe that's a good signal that it's time to rewrite it in C.

You can do with simple IO redirection. For example, the arbitrary pipeline

    $ cat data.txt | awk '{ print $2+$4,$0 }'|sort|sed '/^0/d'
can be written as

    $ 

Re: Useful Unix commands for data science

#45
> cat data.csv | awk -F "|" '{ sum += $4 } END { printf "%.2f\n", sum }'

"Don't pipe a cat".

My test doesn't show a speed improvement, but there are less processes running, and less memory consumed.

  bch:~ bch$ jot 999999999 2 99 > data.dat


  bch:~ bch$ time cat data.dat  | awk '{sum +=$1} END {printf "sum: %d\n", sum}'
  sum: 50499999412

  real 6m21.111s
  user 6m15.506s
  sys  0m5.711s

  PID    COMMAND      %CPU  TIME     #TH   #WQ  #PORTS #MREGS RPRVT  RSHRD  RSIZE  VPRVT  VSIZE  PGRP  PPID  STATE    UID  FAULTS    COW      MSGSENT     MSGRECV
  22342  awk          100.7 05:11.84 1/1   0    17     21     52K    212K   340K   17M    2378M  22341 22306 running  501  311       49       73          36
  22341  cat          1.1   00:03.94 1/1   0    17     21     272K   212K   548K   17M    2378M  22341 22306 running  501  268       51       73          36
==============

  bch:~ bch$ time awk '{sum +=$1} END {printf "sum: %d\n", sum}' ./data.dat
  sum: 50499999412

  real 6m24.023s
  user 6m13.828s
  sys  0m2.774s

  PID    COMMAND      %CPU  TIME     #TH   #WQ  #PORTS #MREGS RPRVT  RSHRD  RSIZE  VPRVT  VSIZE  PGRP  PPID  STATE    UID  FAULTS    COW      MSGSENT     MSGRECV
  22373  awk          100.0 00:30.16 1/1   0    17     21     276K   212K   624K   17M    2378M  22373 22306 running  501  256       46       73          36

Re: Useful Unix commands for data science

#47
post #45

> cat data.csv | awk -F "|" '{ sum += $4 } END { printf "%.2f\n", sum }' "Don't pipe a cat". My test doesn't show a speed improvement, but there are less processes running, and less memory consumed. bch:~ bch$ jot 999999999 2 99 > data.dat bch:~ bch$ time cat data.dat | awk '{sum +=$1} END {printf "sum: %d\n", sum}' sum: 50499999412 real 6m21.111s user 6m15.506s sys 0m5.711s PID COMMAND %CPU TIME #TH #WQ #PORTS #MREG…

Sometimes I like to start with cat so I can easily swap for zcat when changing to gripped input.

Re: Useful Unix commands for data science

#48
One trick I like to do is to feed two files into awk; /dev/stdin and some other file I'm interested in. Here's an example: lookup the subject names of a list of serial numbers in an openSSL index.txt

    #!/bin/sh
    
    printf %s\\n "$@" | awk -F'\t' '
        FILE == "/dev/stdin" {
            needle[$0] = 1
            next
        }

        needle[$4] {
            print $NF
        }
    ' /dev/stdin /etc/pki/CA/index.txt
I find myself using this idiom (feeding data from a file to awk and selecting it with data from standard input) again and again. It's a great way to scale shell scripts to take multiple arguments while avoiding opening the same file N times, or doing clunky things with awk's -v flag.

Re: Useful Unix commands for data science

#49
Wow, I have to say I use all these commands - these are also particularly useful while testing Hadoop streaming jobs since you can test locally on your shell using "cat | map | sort | reduce" (replace cat with head if you want) and then actually run it in Hadoop.

Re: Useful Unix commands for data science

#50
post #39

Earlier quoted context omitted.

>I recommend "The AWK Programming Language" by Aho, Kernighan, and Weinberger I concur with this recommendation. "The AWK Programming Language", at little over 100 pages, is a classic of programming language instruction. The book jumps right into use cases, it does not waste one's time. This book should be required reading for anyone contemplating writing a handbook on any programming language; my CS bookshelf would…

Sadly it seems very expensive now, $95 on Amazon...

USD 8.99 used, with 3.99 shipping.
Post reply on HN