方法一
#!/bin/bash
# 判断ip格式
grep "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$" /root/ip.txt > /root/ip_test1.txt
line=$(wc -l /root/ip_test1.txt | awk ' {print $1} ')
#统计test1 中有几行
echo "" > /root/ip_test.txt
#清空最终数据文件
for((i=1;i<=$line;i=i+1 ))
do
cat /root/ip_test1.txt | awk 'NR=='$i{print}'' > /root/ip_test2.txt
#每次从粗过滤的结果中取出一个放在临时文件 test2中 然后在截取进行判段
a=$(cat /root/ip_test2.tst| cut -d '.' -f 1)
b=$(cat /root/ip_test2.tst| cut -d '.' -f 1)
c=$(cat /root/ip_test2.tst| cut -d '.' -f 1)
d=$(cat /root/ip_test2.tst| cut -d '.' -f 1)
#分别取出ip的4部分,进行判断
if [ "$a" -lt 1 -o -gt 255 ]
#如果第一个数小于1或者大于255 大于等于是-ge
then
continue
#结束本次循环
fi
if [ "$b" -lt 1 -o -gt 255 ]
#如果第一个数小于1或者大于255 大于等于是-ge
then
continue
#结束本次循环
fi
i