比较 Linux 中的 Txt 文件并返回文件 1 中的元素作为文件 2 中的元素

发布于 2024-11-07 19:29:36 字数 885 浏览 7 评论 0原文

问题就在这里。

  1. 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 的文本文件中,

  1. 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.

  1. 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

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

眸中客 2024-11-14 19:29:37

我使用Python的看法:

hosts_contents = open('Host.txt', 'r').read()
hosts_and_ips_contents = open('HosandIp.txt', 'r').read()
host_ips = dict(line.split(',') for line in hosts_and_ips_contents.splitlines())
hosts_wanted = set(hosts_contents.splitlines())
print '\n'.join(ip for host, ip in host_ips.iteritems() if host in hosts_wanted)

My take using Python:

hosts_contents = open('Host.txt', 'r').read()
hosts_and_ips_contents = open('HosandIp.txt', 'r').read()
host_ips = dict(line.split(',') for line in hosts_and_ips_contents.splitlines())
hosts_wanted = set(hosts_contents.splitlines())
print '\n'.join(ip for host, ip in host_ips.iteritems() if host in hosts_wanted)
提赋 2024-11-14 19:29:37

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

梦过后 2024-11-14 19:29:37

单行:

for line in $(cat host.txt); do sed -n s/$line',\(.*\)/\1/p' host-and-ip.txt ; done > ip-only.txt

以可读形式:

for line in $(cat host.txt)
do
  sed -n s/$line',\(.*\)/\1/p' host-and-ip.txt 
done > ipOnly.txt

one-liner:

for line in $(cat host.txt); do sed -n s/$line',\(.*\)/\1/p' host-and-ip.txt ; done > ip-only.txt

in readable form:

for line in $(cat host.txt)
do
  sed -n s/$line',\(.*\)/\1/p' host-and-ip.txt 
done > ipOnly.txt
戏舞 2024-11-14 19:29:36
sort Host.txt -o Host_sorted.txt
sort HosandIp.txt -o HosandIp_sorted.txt
join Host_sorted.txt HosandIp_sorted.txt -t, | cut -d, -f2

输入文件必须排序。这些条目并未完全按照 join 所需的顺序排序,因此我包含了对 sort 的调用来执行此操作。 加入手册页

sort Host.txt -o Host_sorted.txt
sort HosandIp.txt -o HosandIp_sorted.txt
join Host_sorted.txt HosandIp_sorted.txt -t, | cut -d, -f2

The input files must be sorted. The entries aren't quite sorted in the order desired by join, therefore I've included the calls to sort to do this. Join man page

懵少女 2024-11-14 19:29:36

这是我的 0.02$ perl

#!/usr/bin/perl
use strict;
use warnings;

open (HOST, '<Host.txt') || die;
open (HOSTANDIP, '<HostAndIp.txt') || die;

my %host2ip;
map { chomp; my ($h,$i) = split /,/; $host2ip{$h} = $i } (<HOSTANDIP>);

map { chomp; print "$host2ip{$_}\n" if exists($host2ip{$_}) } (<HOST>);

Here is my 0.02$ in perl

#!/usr/bin/perl
use strict;
use warnings;

open (HOST, '<Host.txt') || die;
open (HOSTANDIP, '<HostAndIp.txt') || die;

my %host2ip;
map { chomp; my ($h,$i) = split /,/; $host2ip{$h} = $i } (<HOSTANDIP>);

map { chomp; print "$host2ip{$_}\n" if exists($host2ip{$_}) } (<HOST>);
弱骨蛰伏 2024-11-14 19:29:36
awk -F, '
  NR == FNR {host[$1]++; next}
  ($1 in host) {print $2}
' Host.txt HostandIP.txt > IPOnly.txt
awk -F, '
  NR == FNR {host[$1]++; next}
  ($1 in host) {print $2}
' Host.txt HostandIP.txt > IPOnly.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文