码迷,mamicode.com
首页 > 其他好文 > 详细

awk函数实现将简化IPV6地址补全

时间:2018-12-27 03:10:46      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:turn   i++   ica   $2   ++   切割   cas   $nf   pre   

在用awk处理文本时,有些场景需要将简化的IPV6地址补充成完整的IPV6地址,下边函数可简单实现:

  • IPV6地址补全函数
# ipv6地址补全函数
function compipv6(orig_address){
    # 分割IPV6地址
    split(orig_address, ipv6_addr, "/")
    n = split(ipv6_addr[1], ip_field, ":")
    full_addr=""
    # 切割简化的地址
    split(ipv6_addr[1], ip_field, ":")
    # 每个字段不足4位则高位补0
    for ( i=1; i<=n; i++){
        if ( length(ip_field[i]) == 0 ){
            ip_field[i] = "0000"
        }
        else if ( length(ip_field[i]) == 1 ){
            ip_field[i] = "000"ip_field[i]
        }
        else if ( length(ip_field[i]) == 2 ){
            ip_field[i] = "00"ip_field[i]
        }
        else if ( length(ip_field[i]) == 3 ){
            ip_field[i] = "0"ip_field[i]
        }
        # 组合临时简化的IPV6地址
        if ( i==1 ){
            full_addr = ip_field[i]
        }else{
            full_addr = full_addr":"ip_field[i]
        }
    }
    # 循环补全32位
    do{
        FS = ":"
        $0 = full_addr
        if( $(NF-1) == "0000" && $NF == "0000" ){
            n1 = 8-NF
            for ( i=1; i<=n1; i++ ){
                full_addr=full_addr":0000"
            }
        }
        else if( $NF != "0000" ){
            n1 = 8-NF
            for ( i=1; i<=n1; i++ ){
                full_addr=gensub(/0000/,"0000:0000",1,full_addr)
            }
        }
        FS=" "
    }while(0)
    return full_addr"/"ipv6_addr[2]
}
  • 需要解析适配的源数据
 vrf ChinaMobile_SGi
  rd 24059:4600
  address-family ipv6 unicast
   network 2409:8005:800::/127
   network 2409:8005:800::8/127
   network 2409:8005:800::10/127
   network 2409:8005:800::1c/127
   network 2409:8005:800::24/127
   network 2409:8015:800::/120
   network 2409:8015:800::100/120
   network 2409:8015:800:1::/64
   network 2409:8015:800:2::/64
   network 2409:8015:870::/44
   network 2409:8015:880::/43
   network 2409:8127:f200::/44
   network 2409:8127:f210::/44
   network 2409:8127:f220::/44
   network 2409:8127:f240::/44
   network 2409:8805:800::/43
   network 2409:8805:820::/43
   network 2409:8805:840::/44
   network 2409:8805:850::/44
   network 2409:8805:860::/43
   network 2409:8805:8a0::/43
   network 2409:8805:8c0::/43
  !
 !
  • awk完整程序
#!/bin/awk -f 
# author: wholj
# date: 2018-12-21

# ipv6地址补全函数
function compipv6(orig_address){
    # 分割IPV6地址
    split(orig_address, ipv6_addr, "/")
    n = split(ipv6_addr[1], ip_field, ":")
    full_addr=""
    # 切割简化的地址
    split(ipv6_addr[1], ip_field, ":")
    # 每个字段不足4位则高位补0
    for ( i=1; i<=n; i++){
        if ( length(ip_field[i]) == 0 ){
            ip_field[i] = "0000"
        }
        else if ( length(ip_field[i]) == 1 ){
            ip_field[i] = "000"ip_field[i]
        }
        else if ( length(ip_field[i]) == 2 ){
            ip_field[i] = "00"ip_field[i]
        }
        else if ( length(ip_field[i]) == 3 ){
            ip_field[i] = "0"ip_field[i]
        }
        # 组合临时简化的IPV6地址
        if ( i==1 ){
            full_addr = ip_field[i]
        }else{
            full_addr = full_addr":"ip_field[i]
        }
    }
    # 循环补全32位
    do{
        FS = ":"
        $0 = full_addr
        if( $(NF-1) == "0000" && $NF == "0000" ){
            n1 = 8-NF
            for ( i=1; i<=n1; i++ ){
                full_addr=full_addr":0000"
            }
        }
        else if( $NF != "0000" ){
            n1 = 8-NF
            for ( i=1; i<=n1; i++ ){
                full_addr=gensub(/0000/,"0000:0000",1,full_addr)
            }
        }
        FS=" "
    }while(0)
    return full_addr"/"ipv6_addr[2]
}

# awk开始处理传入的文本文件
{
    # 将文件中的\r换行替换为\n
    gsub("\r","\n",$0)
    if( NF==2 && $1~/network/ ){
        # 调用补全函数将IPV6地址补全
        print $2" --> "compipv6($2)
    }
}
  • 解析适配结果
2409:8005:800::/127 --> 2409:8005:0800:0000:0000:0000:0000:0000/127
2409:8005:800::8/127 --> 2409:8005:0800:0000:0000:0000:0000:0008/127
2409:8005:800::10/127 --> 2409:8005:0800:0000:0000:0000:0000:0010/127
2409:8005:800::1c/127 --> 2409:8005:0800:0000:0000:0000:0000:001c/127
2409:8005:800::24/127 --> 2409:8005:0800:0000:0000:0000:0000:0024/127
2409:8015:800::/120 --> 2409:8015:0800:0000:0000:0000:0000:0000/120
2409:8015:800::100/120 --> 2409:8015:0800:0000:0000:0000:0000:0100/120
2409:8015:800:1::/64 --> 2409:8015:0800:0001:0000:0000:0000:0000/64
2409:8015:800:2::/64 --> 2409:8015:0800:0002:0000:0000:0000:0000/64
2409:8015:870::/44 --> 2409:8015:0870:0000:0000:0000:0000:0000/44
2409:8015:880::/43 --> 2409:8015:0880:0000:0000:0000:0000:0000/43
2409:8127:f200::/44 --> 2409:8127:f200:0000:0000:0000:0000:0000/44
2409:8127:f210::/44 --> 2409:8127:f210:0000:0000:0000:0000:0000/44
2409:8127:f220::/44 --> 2409:8127:f220:0000:0000:0000:0000:0000/44
2409:8127:f240::/44 --> 2409:8127:f240:0000:0000:0000:0000:0000/44
2409:8805:800::/43 --> 2409:8805:0800:0000:0000:0000:0000:0000/43
2409:8805:820::/43 --> 2409:8805:0820:0000:0000:0000:0000:0000/43
2409:8805:840::/44 --> 2409:8805:0840:0000:0000:0000:0000:0000/44
2409:8805:850::/44 --> 2409:8805:0850:0000:0000:0000:0000:0000/44
2409:8805:860::/43 --> 2409:8805:0860:0000:0000:0000:0000:0000/43
2409:8805:8a0::/43 --> 2409:8805:08a0:0000:0000:0000:0000:0000/43
2409:8805:8c0::/43 --> 2409:8805:08c0:0000:0000:0000:0000:0000/43

awk函数实现将简化IPV6地址补全

标签:turn   i++   ica   $2   ++   切割   cas   $nf   pre   

原文地址:https://www.cnblogs.com/wholj/p/10182599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!