I want to find partially matching ipv6 prefixes in two arrays. For instance, 2001:db8:
from one array will match 2001:db8:1::/48
and 2001:db8:2::/48
from another.
I already have it working by iterating one array other another:
ru_routes=( $(curl -4 ftp://ftp.ripe.net/ripe/stats/delegated-ripencc-latest | egrep -o '\|RU\|ipv6\|.+?::\|[0-9]+' | cut -d'|' -f4 | sed 's/::$/:/g') );
msk_ix_routes=( $(curl -4 http://www.msk-ix.ru/download/lg/msk_ipv6_pfx.txt.gz | gunzip | egrep -o '\b.*::/[0-9]*') );
routes=();
for item1 in ${msk_ix_routes[@]}; do
for item2 in ${ru_routes[@]}; do
if [[ $item1 = $item2* ]]; then
routes+=( $item1 );
break
fi
done
done
But it works kinda slow on my mips router (~90sec). I found this useful answer, which runs much faster but I cannot get it to work same way as the one above. And I don't think I need "if" construction as in example, because it will do the same thing twice. My not-working version:
msk=" ${msk_ix_routes[*]} "; # add framing blanks
for item in ${ru_routes[@]}; do
routes+=( egrep -o "$item[\S]*/g" <<< $msk );
done
I guess there are problems with quoting and escaping here, but I cannot solve it. Please help) I am open to suggestions.
Btw, I used "comm" in first version which runs even faster, but then it does exact match only, hence I started to play with loops:
routes=( $(comm -12 <(printf '%s\n' "${ru_routes[@]}" | LC_ALL=C sort) <(printf '%s\n' "${msk_ix_routes[@]}" | LC_ALL=C sort)) );