我使用这个脚本来在线发现IP
#!/bin/sh
set -e
# no args
if [ $# -lt 1 ]; then
echo "Too few args"
echo "Options are:"
echo "-a: Tell me which host is online"
echo "-b: show only list of ip found"
exit 1
fi
# too many args
if [ $# -gt 1 ]; then
echo "$1 option unknown"
echo "Options are:"
echo "-a: Tell me which host is online"
echo "-b: show only list of ip found"
exit 1
fi
case $1 in
-a)
array1=(
`nmap -sP 192.168.0.0/24 | awk '/is up/ {print up}; {gsub (/\(|\)/,""); up = $NF}'`
)
for i in ${array1[@]};do echo "Ip $i is online";done
;;
-b)
nmap -sP 192.168.0.0/24 | awk '/is up/ {print up}; {gsub (/\(|\)/,""); up = $NF}'|sort -fn
;;
*)
echo "$1 option unknown"
echo "Options are:"
echo "-a: Tell me which host is online"
echo "-b: show only list of ip found"
;;
esac
但使用 -b 返回这样的列表
192.168.0.1
192.168.0.11
192.168.0.14
192.168.0.15
192.168.0.17
192.168.0.2
192.168.0.3
192.168.0.44
192.168.0.46
192.168.0.49
192.168.0.50
我想要这样的列表排序
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.11
192.168.0.14
192.168.0.15
192.168.0.22
192.168.0.44
192.168.0.46
192.168.0.49
192.168.0.50
有什么排序建议吗?
答案1
sort
.
仅根据分隔的第四个字段:
... | sort -t. -k4,4n
-t.
将输入字段分隔符设置为.
-
-k4,4n
仅根据第4个字段对文件进行排序,并n
实现数字排序
例子:
$ cat file.txt
192.168.0.1
192.168.0.11
192.168.0.14
192.168.0.15
192.168.0.17
192.168.0.2
192.168.0.3
192.168.0.44
192.168.0.46
192.168.0.49
192.168.0.50
$ sort -t. -k4,4n file.txt
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.11
192.168.0.14
192.168.0.15
192.168.0.17
192.168.0.44
192.168.0.46
192.168.0.49
192.168.0.50