为了账号安全,请及时绑定邮箱和手机立即绑定

如何将文件中的 IP 地址列表从 xxxx-xxxx 扩展为 xxxx-x 格式?

如何将文件中的 IP 地址列表从 xxxx-xxxx 扩展为 xxxx-x 格式?

MM们 2023-12-09 14:52:44
IP范围10.0.0.1-10.0.0.3可扩展至10.0.0.110.0.0.210.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.3172.16.0.3192.168.0.2-192.168.0.2我如何产生这样的输出?10.0.0.110.0.0.210.0.0.3172.16.0.3192.168.0.2
查看完整描述

1 回答

?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

您能否尝试以下操作,仅在 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.


查看完整回答
反对 回复 2023-12-09
  • 1 回答
  • 0 关注
  • 43 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信