我已经尝试了多次尝试在多个文件中grep ip地址的前三个八位字节,但是当第一个八位字节是一个或两个数字时,它有时会捕获误报。
shell为“SH”
(示例INPUT)
i = 5.79.78
$ data中的for i收集了需要处理的多个IP地址。为简单起见 我只显示一个“i”值。
码
max=5
data="$(cat $dir* | cut -d '.' -f 1-3 | awk -v max="$max" '{a[$0]++}END{for(i in a){if(a[i] > max){print i}}}' | sed 's/.*://')"
for i in $data; do
ii="${i}."
list=$(grep "$ii" $dir*)
done
echo "$list"
* 输出*
/tmp/aaa.txt:5.79.78.237
/tmp/aaa.txt:5.79.78.230
/tmp/aaa.txt:5.79.78.229
/tmp/aaa.txt:5.79.78.228
/tmp/aaa.txt:5.79.78.227
/tmp/ddd.txt:5.79.78.236
/tmp/sss.txt:95.79.78.95 False
/tmp/sss.txt:95.79.78.66 False
/tmp/sss.txt:95.79.78.48 False
/tmp/sss.txt:95.79.78.216 False
/tmp/sss.txt:95.79.78.163 False
但它在我正在寻找的地址之前用数字拉取IP地址。 我试图添加一个尾随“。”但它仍然无法找到所有正确的匹配。
尝试
list=$(grep -E "$i""\.[0-9]{1,3}" $dir*)
list=$(grep -e ^ "$ii" $dir*)
list=$(grep "\<$ii" $dir*)
list=$(grep "^$ii" $dir*)
(Also tried list="$grep "^$ii" $dir*)" (adding quotes)
答案 0 :(得分:1)
供您输入
while read -r input
do
if [ -z "$input" ]
then
continue
fi
ip3=`echo "$input." | sed 's/.*= *//'`
regex=`echo $ip3 | sed 's/\./\\\./g'`
grep -H "^$regex" /tmp/*.txt
done <<EOF
i = 5.79.78
i = 5.34.244
EOF
如果/tmp/a.txt包含
5.79.78.236
95.79.78.95
将打印
/tmp/a:5.79.78.236
答案 1 :(得分:0)
#!/bin/sh
data="5.79.78 5.34.244"
octet_re='([1-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
n=0
for prefix in $data; do
n=$(expr $n + 1)
echo "run $n"
prefix=$( echo "$prefix" | sed 's/\./\\./g' )
output=$( egrep "\<${prefix}\.${octet_re}\>" <<END
yes 5.79.78.123
no 95.79.78.124
no 5.34.244.256
no 15.34.244.127
yes 5.34.244.1
no 5-34.244.1
END
)
echo "$output"
done
run 1
yes 5.79.78.123
run 2
yes 5.34.244.1