I have two files, file1.csv
3 1009
7 1012
2 1013
8 1014
and file2.csv
5 1009
3 1010
1 1013
In the shell, I want to subtract the count in the first column in the second file from that in the first file, based on the identifier in the second column. If an identifier is missing in the second column, the count is assumed to be 0.
The result would be
-2 1009
-3 1010
7 1012
1 1013
8 1014
The files are huge (several GB). The second columns are sorted.
How would I do this efficiently in the shell?
Assuming this is a csv with blank separation, if this is a "," use argument
-F ','
for memory issue (could be in 1 serie of pipe but prefer to use a temporary file)
Assuming that both files are sorted on second column:
join
will join sorted files.-j2
will join one second column.-a1
will print records from file1 even it there is no corresponding row in file2.-a2
Same as-a1
but applied for file2.-oauto
is in this case the same as-o1.2,1.1,2.1
which will print the joined column, and then the remaining columns from file1 and file2.-e0
will insert0
instead of an empty column. This works with-a1
and-a2
.The output from
join
is three columns like:Which is piped to awk, to subtract column three from column 2, and then reformatting.
Since the files are sorted¹, you can merge them line-by-line with the
join
utility incoreutils
:All those options are required:
-j2
says to join based on the second column of each file-o auto
says to make every row have the same format, beginning with the join key-e 0
says that missing values should be substituted with zero-a 1
and-a 2
include rows that are absent from one file or anotherNow we have a stream of output in that format, we can do the subtraction on each line. I used this GNU sed command to transform the above output into a
dc
program:This takes the three values on each line and rearranges them into a
dc
command for the subtraction, then executes it. For example, the first line becomes (with spaces added for clarity)which subtracts 5 from 3, prints it, then prints a space, then prints 1009 and a newline, giving
as required.
We can then pipe all these lines into
dc
, giving us the output file that we want:¹ The sorting needs to be consistent with
LC_COLLATE
locale setting. That's unlikely to be an issue if the fields are always numeric.TL;DR
The full command is:
It works a line at a time, and starts only the three processes you see, so should be reasonably efficient in both memory and CPU.
It reads the first file in memory so you should have enough memory available. If you don't have the memory, I would maybe
sort -k2
the files first, thensort -m
(merge) them and continue with that output:(I'm out of time for now, maybe I'll finish it later)
EDIT by Ed Morton Hope you don't mind me adding what I was working on rather than posting my own extremely similar answer, feel free to modify or delete it: