1

我想在两个数组中找到部分匹配的 ipv6 前缀。例如,一个2001:db8:数组将匹配另一个数组。2001:db8:1::/482001:db8:2::/48

我已经通过迭代另一个数组来让它工作:

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

但它在我的 mips 路由器上运行有点慢(~90 秒)。我找到了这个有用的答案,它运行得更快,但我无法让它以与上述相同的方式工作。而且我认为我不需要像示例中那样的“if”构造,因为它会两次做同样的事情。我的不工作版本:

msk=" ${msk_ix_routes[*]} ";         # add framing blanks

for item in ${ru_routes[@]}; do
  routes+=( egrep -o "$item[\S]*/g" <<< $msk );
done

我想这里引用和转义存在问题,但我无法解决。请帮助)我愿意接受建议。

顺便说一句,我在第一个版本中使用了“comm”,它运行得更快,但它只完全匹配,因此我开始玩循环:

routes=( $(comm -12 <(printf '%s\n' "${ru_routes[@]}" | LC_ALL=C sort) <(printf '%s\n' "${msk_ix_routes[@]}" | LC_ALL=C sort)) );
4

1 回答 1

1

Bash 脚本在效率上根本就不好。尝试这个:

#!/bin/bash

# e. g.: ripencc|RU|ipv6|2001:640::|32|19991115|allocated -> ^2001:640:
awk -v FS='|' \
    '$2 == "RU" && $3 == "ipv6" { sub(/::/, ":", $4); print "^" $4 }' \
    <(curl -4 ftp://ftp.ripe.net/ripe/stats/delegated-ripencc-latest) \
|\
# grep e. g. '^2001:640:' in '2001:640:8000::/33'
grep --basic-regexp --file - \
    <(curl -4 http://www.msk-ix.ru/download/lg/msk_ipv6_pfx.txt.gz | gunzip)
于 2014-09-01T00:06:07.460 回答