【问题标题】:How to expand list of IP Address in a file from x.x.x.x-x.x.x.x to x.x.x.x-x format? [duplicate]如何将文件中的 IP 地址列表从 x.x.x.x-x.x.x.x 扩展为 x.x.x.x-x 格式? [复制]
【发布时间】:2023-04-03 05:06:02
【问题描述】:

10.0.0.1-10.0.0.3的IP范围可以扩展到

10.0.0.1
10.0.0.2
10.0.0.3

通过使用此脚本

eval printf '%s\\n' $(echo '10.0.0.1-10.0.0.3' | awk -F'[.-]' -v OFS='.' '{for(i=1;i<=4;i++) $i = "{" $i ".." $(i+4) "}"; NF=4} 1')

但是如果我有这样的一组数据;

10.0.0.1-10.0.0.3
172.16.0.3
192.168.0.2-192.168.0.2

我如何产生这样的输出?

10.0.0.1
10.0.0.2
10.0.0.3
172.16.0.3
192.168.0.2

【问题讨论】:

    标签: python bash awk


    【解决方案1】:

    您能否尝试仅在 GNU awk 中使用所示示例进行跟踪、编写和测试。

    awk '
    BEGIN{
      FS="-"
      OFS="."
    }
    $1==$2 || NF==1{
      print $1
      next
    }
    {
      num1=split($1,arr1,".")
      num2=split($2,arr2,".")
      for(i=arr1[num1];i<=arr2[num2];i++){
         print arr1[1],arr1[2],arr1[3],i
      }
    }' Input_file
    

    说明:为上述添加详细说明。

    awk '                                     ##Starting awk program from here.
    BEGIN{                                    ##Starting BEGIN section of this program from here.
      FS="-"                                  ##Setting field separator as - here. 
      OFS="."                                 ##Setting output field separator as . here.
    }
    $1==$2 || NF==1{                          ##Checking condition if 1st field is equal to 2nd field OR number of fields is 1 then do following.
      print $1                                ##Printing 1st field of current line here.
      next                                    ##next will skip all further statements from here.
    }
    {
      num1=split($1,arr1,".")                 ##Splitting $1 into arr1 with delimiter . AND num1 will have total number of elements in arr1.
      num2=split($2,arr2,".")                 ##Splitting $2 into arr2 with delimiter . AND num2 will have total number of elements in arr1.
      for(i=arr1[num1];i<=arr2[num2];i++){    ##Running for loop from last value of 1st field to last value of 2nd field.
         print arr1[1],arr1[2],arr1[3],i      ##Printing arr1 1st, 2nd and 3rd value and then printing value of i here.
      }
    }' Input_file                             ##Mentioning Input_file name here.
    

    【讨论】:

      猜你喜欢
      • 2020-06-30
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多