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

IP范围10.0.0.1-10.0.0.3可扩展至


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


MM们
浏览 47回答 1
1回答

天涯尽头无女友

您能否尝试以下操作,仅在 GNU 中使用所示示例进行编写和测试awk。awk 'BEGIN{&nbsp; FS="-"&nbsp; OFS="."}$1==$2 || NF==1{&nbsp; print $1&nbsp; next}{&nbsp; num1=split($1,arr1,".")&nbsp; num2=split($2,arr2,".")&nbsp; for(i=arr1[num1];i<=arr2[num2];i++){&nbsp; &nbsp; &nbsp;print arr1[1],arr1[2],arr1[3],i&nbsp; }}' Input_file说明:对上述内容添加详细说明。awk '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;##Starting awk program from here.BEGIN{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##Starting BEGIN section of this program from here.&nbsp; FS="-"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##Setting field separator as - here.&nbsp;&nbsp; OFS="."&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;##Setting output field separator as . here.}$1==$2 || NF==1{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##Checking condition if 1st field is equal to 2nd field OR number of fields is 1 then do following.&nbsp; print $1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##Printing 1st field of current line here.&nbsp; next&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##next will skip all further statements from here.}{&nbsp; num1=split($1,arr1,".")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;##Splitting $1 into arr1 with delimiter . AND num1 will have total number of elements in arr1.&nbsp; num2=split($2,arr2,".")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;##Splitting $2 into arr2 with delimiter . AND num2 will have total number of elements in arr1.&nbsp; for(i=arr1[num1];i<=arr2[num2];i++){&nbsp; &nbsp; ##Running for loop from last value of 1st field to last value of 2nd field.&nbsp; &nbsp; &nbsp;print arr1[1],arr1[2],arr1[3],i&nbsp; &nbsp; &nbsp; ##Printing arr1 1st, 2nd and 3rd value and then printing value of i here.&nbsp; }}' Input_file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;##Mentioning Input_file name here.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python