linux - 如何在另一个文件中查找 ip 地址列表

标签 linux bash

我的任务是查看我们是否正在公布一个 IP 地址列表 (3000)。手动操作不是个好主意,所以我复制了我们在文件中公布的所有 IP 地址。现在我只需要创建一个 bash 脚本并将 ip 地址列表提供给脚本,这样它就可以在文件中找到带有广告 ip 地址列表的 ip 地址。如果找到,则将其保存在一个文件中,如果不在另一个文件中。这是我到目前为止所拥有的。这个脚本的问题是我必须手动输入每个 ip 地址。我如何在文件中提供要搜索的 IP 地址列表以及我们正在转换广告的 IP 地址列表。非常感谢您。

 #!/bin/bash

while true; do
  echo -e "IP address: \c"
  read ip

  if grep --color "$ip" "ips"; then
    echo $ip "was found"
    echo $ip >> found
  else
    echo $ip "was NOT found"
    echo $ip >> notFound

  fi

done

最佳答案

如果对这两个文件进行排序,可以使用comm命令:

sort all_ip_addresses > all_ip_addresses_sorted
sort adverted_ip_address > advertised_ip_address_unsorted

comm -23 all_ip_addresses_sorted advertised_ip_addresses_sorted

将显示未公布的 IP 地址,并且:

comm -12 all_ip_addresses_sorted advertised_ip_addresses_sorted

将显示公布的 IP 地址。

您还可以通过使用进程替换来避免创建单独的排序文件:

comm -23 <(sort all_ip_addresses) <(sort advertised_ip_addresses)

关于linux - 如何在另一个文件中查找 ip 地址列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21197744/

相关文章:

linux - 如何在 bash 中读取带有冒号的文本文件

bash - 等到docker通过shell脚本在Mac OS中启动?

regex - native bash 正则表达式 [[ $f =~ "^[^\.]+$"]] 从不匹配

linux - 如何在Buildroot中定义局部变量,这是bash脚本的执行结果

linux - 如果命令失败如何退出?

linux - 列出带有 .bar 扩展名但不带有 .foo.bar 扩展名的目录中的所有文件

arrays - 我尝试获取数组长度有什么问题?

windows - 从 Linux 背景学习 Windows 开发

c++ - 在 C++ 程序中使用 C 共享库

linux - 核心操作系统 : when pulling large docker image of size greater than 4 GB