One can simply do awk 'cmds' file.
Useful Unix commands for data science
11–20 of 108 posts
Re: Useful Unix commands for data science
#12I 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…
$ alias sum="xargs | tr ' ' '+' | bc"
$ echo -e "1\n2\n3\n" | sum
6Re: Useful Unix commands for data science
#13I was hoping to see an article about some neat new utilities specifically tailored for doing advanced data analysis. Instead this is a set of basic examples of bog-standard tools that every newbie *nix user should be already familiar with: cat, awk, head, tail, wc, grep, sed, sort, uniq
The key word is should... you might be surprised how many "not newbie" nix users are not aware of those commands or how using them in this fashion. Specially awk.
Re: Useful Unix commands for data science
#14What most users probably don't realize is that the redirection can be anywhere on the line, not just at the beginning. Putting an input redirection at the beginning of the command can make the data flow clearer: from the input file, through the command, to stdout:
(This only works for simple commands; you can't do `< file if blah; then foo; else bar; fi`)Re: Useful Unix commands for data science
#15 Make all your commands 3x faster:
export LC_ALL=C
Actually use the 32 CPUs you paid for:
sort --parallel=32 ...
xargs -P32 ...Re: Useful Unix commands for data science
#16Re: Useful Unix commands for data science
#17AWK is worth learning completely. It hits a real sweet spot in terms of minimizing the number of lines of code needed to write useful programs in the world of quasi-structured (not quite CSV but not completely free form) data. You can learn the whole language and become proficient in an afternoon. I recommend "The AWK Programming Language" by Aho, Kernighan, and Weinberger, though it seems to be listed for a hilariou…
Re: Useful Unix commands for data science
#18BashReduce is a pretty cool application of many of these utilities.
Re: Useful Unix commands for data science
#19Re: Useful Unix commands for data science
#20Please be very careful doing math with bash and awk... cat data.csv | awk -F "|" '{ sum += $4 } END { printf "%.2f\n", sum }' From that command, it's unclear whether the sum will be accurate, it depends on the inputs and on the precision of awk. See (D.3 Floating-Point Number Caveats): http://www.delorie.com/gnu/docs/gawk/gawk_260.html
I don't see how that's more true for awk than it is for any other programming language. Awk uses double precision floating point for all numeric values, which isn't a horrible choice for a catch-all numeric type.