I've got two files (millions of columns)
File1.txt, ~4k rows
some_key1 some_text1
some_key2 some_text2
...
some_keyn some_textn
File2.txt, ~20 M rows
some_key11 some_key11 some_text1
some_key22 some_key22 some_text2
...
some_keynn some_keynn some_textn
When there is an exact match between column 2 in File1.txt
and column 3 in File2.txt
, I want to print out the particular rows from both files.
EDIT
I've tried this (I forgot to write it) but it doesn't work
awk 'NR{a[$2]}==FNR{b[$3]}'$1 in a{print $1}' file1.txt file2.txt
You need to fix your
awk
programTo print all records in file2 if field 1 (file1) exists in field 3 (file2):-
To print just field 1 in file2 if field 1 (file1) exists in field 3 (file2):-
Let's say your dataset is big in both dimensions - rows and columns. Then you want to use
join
. To usejoin
, you have to sort your data first. Something along those lines:The
sort -k2,2
means 'sort whole rows so the values of second column are in ascending order. Thejoin -1 2
means 'the key in the first file is the second column'.If your files are bigger than say 100 MB it pays of to assign additional memory to the
sort
via the-S
option. The rule of thumb is to assign 1.3 times the size of the input to avoid any disk swapping bysort
. But only if your system can handle that.If one of your data files is very small (say up to 100 lines), you can consider doing something like
to avoid the
sort
, but then you'd have to look up the 'keys' from that file.The decision which one to use is very similar to the 'hash join' and 'merge join' in the database world.