2014/02/04
join: the command
From the manual:
1
2
3
4
5
6
|
NAME
join -- relational database operator
SYNOPSIS
join [-a file_number | -v file_number] [-e string] [-o list] [-t char]
[-1 field] [-2 field] file1 file2
|
I had two CSVs, baz01.csv
and baz02.csv
. They shared the same first column, which was a list of database table names. The second column contained the number of rows from each table. The row numbers between the two files were different, and I wanted to compare them. The join
command to the rescue!
1
2
|
join -t , -1 1 -2 1 baz01.csv baz02.csv \
| awk -F, '{print $1","$2","$3", "($3 - $2)}'
|
gave me exactly what I wanted: the output contains the first identical column from both files, followed by column 2 of the baz01.csv
, followed by the second column of baz02.csv
, followed by the third column minus the second.
Of course, this will only work on the simplest CSV files, meaning no escaped or quoted commas allowed.
join: the command is original content from devolve.