これは、メモリの制約により失敗する可能性があります。3 つの列を持つファイル file1 と ID を持つ file2 というファイルを呼び出しました。コード セグメントをコピーしてファイルに貼り付け、必要に応じて名前を編集します。
ステップ 1: ファイル 1 をできるだけ小さくします。
#/bin/bash
declare -a Array
Count=0
最初の 3 番目の列は不要なので、それらを削除し、ファイルを並べ替えてから、一意のエントリのみを取得します。
InitFile ()
{
while IFS=, read ignore1 stuff ignore2; do echo $stuff ; done < file1| sort -n | uniq > $1
}
配列に読み込む:
InitArray ()
{
while read Array[$Count]; do
let Count++
done < $1
}
配列内の値のバイナリ検索:
BinarySearch ()
{
val=$1
let idx=$Count/2
top=$Count
bottom=0
while true; do
if [ ${Array[$idx]} -eq $val ]; then return 0; fi
lastIdx=$idx
if [ $top -le $bottom ]; then return 1; fi
if [ $val -lt ${Array[$idx]} ]; then top=$idx && let idx=$idx/2;
elif [ $val -gt ${Array[$idx]} ]; then bottom=$idx && let idx=($top+$bottom)/2; fi
if [ $idx -eq $lastIdx ]; then let bottom=$bottom+1 ; fi
done
}
uniqueOwnerIdFile は最初のファイルから作成され、配列に入れられます
InitFile uniqueOwnerIdFile
InitArray uniqueOwnerIdFile
2 番目のファイルの各行をループし、所有者 ID 配列で両方の値を探します。見つかったすべてを linesTheExistFile にエコーします。
while IFS=, read firstVal secondVal; do
if BinarySearch $firstVal && BinarySearch $secondVal ; then echo "$firstVal,$secondVal" ; fi
done < file2 > linesThatExistFile