我有两个文件 nmap.txt 和 hosts.txt 。 当然,第一个是nmap搜索的输出,看起来像这样:
====================================
192.168.2.1 (ComputerName1)
Running: Microsoft Windows Vista/7/8
====================================
192.168.2.2 (ComputerName2)
Running: Running: Linux 3.X
====================================
192.168.2.3 ()
====================================
192.168.2.4 ()
它看起来像这样,因为第3和第4台计算机都没有DNS条目,并且nmap无法确定其操作系统的100%。但是我在hosts.txt文件中有计算机名称3和4。
192.168.2.3 (ComputerName3)
192.168.2.4 (ComputerName4)
最简单的方法是将nmap.txt中丢失的计算机名称(192.168.2.3和192.168.2.4)替换为hosts.txt中的相应名称?
我尝试了 grep -v -F -f nmap.txt hosts.txt> output.txt ,它只会输出缺少的计算机名称,如下所示:
192.168.2.3 (ComputerName3)
192.168.2.4 (ComputerName4)
但这不是我想要的。 输出应如下所示:
====================================
192.168.2.1 (ComputerName1)
Running: Microsoft Windows Vista/7/8
====================================
192.168.2.2 (ComputerName2)
Running: Running: Linux 3.X
====================================
192.168.2.3 (ComputerName3)
====================================
192.168.2.4 (ComputerName4)
也许与边读边同时出现,但我不是专家。 任何帮助将不胜感激!
答案 0 :(得分:1)
$ awk 'NR==FNR{map[$1]=$2;next} ($1 in map) && sub(/\(\)$/,""){$0=$0 map[$1]} 1' hosts.txt nmap.txt
====================================
192.168.2.1 (ComputerName1)
Running: Microsoft Windows Vista/7/8
====================================
192.168.2.2 (ComputerName2)
Running: Running: Linux 3.X
====================================
192.168.2.3 (ComputerName3)
====================================
192.168.2.4 (ComputerName4)
答案 1 :(得分:0)
awk:
$ awk 'NR==FNR{a[$1]=$0;next}{print ($1 in a?a[$1]:$0)}' hosts nmap
输出:
====================================
192.168.2.1 (ComputerName1)
Running: Microsoft Windows Vista/7/8
====================================
192.168.2.2 (ComputerName2)
Running: Running: Linux 3.X
====================================
192.168.2.3 (ComputerName3)
====================================
192.168.2.4 (ComputerName4)
解释:
$ awk '
NR==FNR { # process the hosts file
a[$1]=$0 # hash records to a using ip as key
next
}
{ # process nmap file
print ($1 in a?a[$1]:$0) # print hashed record instead if it exists
}' hosts nmap