比较 Linux 中的 Txt 文件并返回文件 1 中的元素作为文件 2 中的元素
问题就在这里。
- Host.txt 文件仅具有主机名,如下所示:
GVC-CH-ZRH-BRA-5H81-PELE GVC-US-NYC-9TH-4C101-MEDROOM GVC-US-NYC-9TH-4C101E-EXTRA GVC-US-NYC-9TH-5E117-STUDIO54
2.HosandIp.txt 具有主机名和 Ip,如下所示(HostandIP 有主机名,IP 地址,注意逗号 (,)):
GVC-CH-ZRH-BRA-5H81-PELE,170.16.75.101 GVC-US-NYC-9TH-4C101-MEDROOM,170.26.114.242 GVC-US-NYC-9TH-4C101E-EXTRA,170.26.108.224 GVC-US-NYC-9TH-5E117-STUDIO54,170.26.108.95 beta-gvc-personal-antoniop-526,170.26.107.180 beta-gvc-personal-antoniop-9100,170.26.106.206 beta-gvc-personal-antoniop-9100b,170.26.106.41 beta-gvc-personal-antoniop-office,170.26.107.192
我需要比较这两个文件并仅获取 IP 地址在另一个名为 IPOnly.txt 的文本文件中,
- IPOnly.txt 仅具有 Host.txt 和 HostandIp.txt 的公共 IP,如下所示:
170.16.75.101 170.26.114.242 170.26.108.224 170.26.108.95
可以使用 HASH MAP 在 JAVA 中完成。 Linux 有命令可以做到这一点吗?请帮忙!
Here is the problem.
- Host.txt file has only Hostnames as follows:
GVC-CH-ZRH-BRA-5H81-PELE GVC-US-NYC-9TH-4C101-MEDROOM GVC-US-NYC-9TH-4C101E-EXTRA GVC-US-NYC-9TH-5E117-STUDIO54
2.HosandIp.txt has the Hostnames and Ip as follows (HostandIP has hostname,IP Address Note the Comma (,)):
GVC-CH-ZRH-BRA-5H81-PELE,170.16.75.101 GVC-US-NYC-9TH-4C101-MEDROOM,170.26.114.242 GVC-US-NYC-9TH-4C101E-EXTRA,170.26.108.224 GVC-US-NYC-9TH-5E117-STUDIO54,170.26.108.95 beta-gvc-personal-antoniop-526,170.26.107.180 beta-gvc-personal-antoniop-9100,170.26.106.206 beta-gvc-personal-antoniop-9100b,170.26.106.41 beta-gvc-personal-antoniop-office,170.26.107.192
I need to compare these two files and get only the IP Address in another text file called IPOnly.txt
- IPOnly.txt has only the IP's common to both Host.txt and HostandIp.txt as follows:
170.16.75.101 170.26.114.242 170.26.108.224 170.26.108.95
It can be done in JAVA using HASH MAP. Is there a Linux Command to do this ? Please Help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我使用Python的看法:
My take using Python:
sed -e 's/^/\^/' -e 's/$/,/' Host.txt | egrep -f - HosandIp.txt | |egrep -f - HosandIp.txt | awk -F, '{print $2}' > IPOnly.txt
sed -e 's/^/\^/' -e 's/$/,/' Host.txt | egrep -f - HosandIp.txt | awk -F, '{print $2}' > IPOnly.txt
单行:
以可读形式:
one-liner:
in readable form:
输入文件必须排序。这些条目并未完全按照
join
所需的顺序排序,因此我包含了对sort
的调用来执行此操作。 加入手册页The input files must be sorted. The entries aren't quite sorted in the order desired by
join
, therefore I've included the calls tosort
to do this. Join man page这是我的 0.02$ perl
Here is my 0.02$ in perl