我有以下情况 我有一个输入文件:
start-I/p end-I/p code
10.35.210.1 10.35.210.255 User1
10.35.145.1 10.35.145.255 User2
我想以下列格式生成ip地址表
ip code
10.35.210.1 user1
10.35.210.2 user1
10.35.210.3 user1
10.35.210.4 user1
...
10.35.210.255 user1
10.35.145.1 user2
10.35.145.2 user2
10.35.145.3 user2
10.35.145.4 user2
...
10.35.145.255 user2
任何人都可以帮我找到合适的解决方案吗?
答案 0 :(得分:0)
以下是您的解决方案,使用sed
和seq
作为依赖关系,请在使用之前尝试理解代码。
#!/bin/bash
echo "ip code";
while read start end code
do
if [[ $start != "start-I/p" ]]
then
startNum=0;
for i in $(echo $start| sed "s/\./ /g")
do
#echo $i;
(( startNum *= 256 ));
(( startNum += $i ));
done
endNum=0;
for i in $(echo $end| sed "s/\./ /g")
do
#echo $i;
(( endNum *= 256 ));
(( endNum += $i ));
done
for j in $(seq $startNum $endNum)
do
(( p1 = $j % 256 ));
(( j /= 256 ));
(( p2 = $j % 256 ));
(( j /= 256 ));
(( p3 = $j % 256 ));
(( j /= 256 ));
(( p4 = $j % 256 ));
echo "$p4.$p3.$p2.$p1 $code";
done
#echo $num1;
fi
done < $1