Matthew Todd

ARGF

Every few months or so, I get to help email out the Amani newsletters by taking everyone’s address books and merging them together. (It’s not ideal; requiring unsubscribing to be a manual process makes it all too easy to forget and annoy some people. Changes are afoot.)

Playing with files like this, it’s fun to drop to the command line and see how quickly I can get it done. This time around, I remembered Ruby’s ARGF, which is a synonym for $<, which clearly came from Perl.

From the Pickaxe, $< is:

An object that provides access to the concatenation of all the files given as command-line arguments or $stdin (in the case where there are no arguments). $< supports methods similar to a File object…

So, here we go:

ruby -rcsv -e 'CSV::Reader.parse(ARGF) { |row| puts row[4] }' *.csv
  | sort | uniq > final.csv

In English, that’s “loading the csv library, parse the given files (*.csv), printing only column 4 of each row. Sort the results, removing duplicates, and save as final.csv.”

I love this stuff.

Comments