Using column 2 of one file to match with two columns of another file, and append
Pipe a modified form of the second file to BEDOPS bedmap and the first file, then pipe that result to cut out desired columns:

$ awk -v OFS='\t' '{ print $1, $2, ($2+1), $3, $4, $5, $6, $7, $8, $9, $10; }' second.txt | bedmap --echo --echo-map first.bed - | cut -f1-4,8,9,11- - &gt; answer.bed


This should run pretty quickly and use very little memory, as it uses Unix streams on (presumably) sorted BED data.

You'll want to test the column indices in my answer by running this on your test inputs, but that's an easy adjustment for the larger dataset, in case I missed a column.

If you're not sure about sort order, pipe to sort-bed:

$ sort-bed first.unsorted.bed &gt; first.bed
$ awk -v OFS='\t' '{ print $1, $2, ($2+1), $3, $4, $5, $6, $7, $8, $9, $10; }' second.txt | sort-bed - | bedmap --echo --echo-map first.bed - | cut -f1-4,8,9,11- - &gt; answer.bed


Use streams where you can. It's a different way of thinking but can pay huge dividends.