我用来存档一个包含IP的主机名,如/ ec / hosts 另一个包含IP和计数器尝试打印ip如果找不到主机名,如果找到则打印主机名。
脚本:
for i in `cat ip | awk '{print $2}'` ;do
var=`grep "$i" Server_ip` |
awk ' {if($var == "") print $i else print $1}';
done
档案1
localhost 127.0.0.1
test 10.0.0.1
test1 10.0.0.2
文件2
3 127.0.0.1
2 10.0.0.1
1 10.0.0.2
4 10.0.0.3
5 10.0.0.4
期望的输出
localhost
test
test1
10.0.0.3
10.0.0.4
答案 0 :(得分:0)
你可以试试这个;
#!/bin/bash
for i in `cat ip | awk '{print $2}'` ;do
var=`awk -v ip=${i} '$2 == ip {print $1}' Server_ip`
if [ -z "$var" ]; then
echo $i
else
echo "$var"
fi
done
例如
user@host:/tmp/test$ cat Server_ip
localhost 127.0.0.1
test 10.0.0.1
test1 10.0.0.2
user@host:/tmp/test$ cat ip
3 127.0.0.1
2 10.0.0.1
1 10.0.0.2
4 10.0.0.3
5 10.0.0.4
user@host:/tmp/test$ ./test.sh
localhost
test
test1
10.0.0.3
10.0.0.4