How to find a list of ip addresses in another file

后端 未结 3 405
我在风中等你
我在风中等你 2020-12-22 07:21

I was given the task to see if we are advertising a list of ip addresses(3000). Not a good idea to do it manually, so I copied all the ip addresses that we are advertising i

相关标签:
3条回答
  • 2020-12-22 07:52

    A better script :

    while read ip  
    do  
        grep "$ip"  "$ips" > /dev/null 2>&1 && echo "$ip" >> ip.found || echo "$ip" >> ip.notfound  
    done    
    

    Name the script "searchip.sh"
    Assume your input file is "iplist" ,set up variable and call like this:

    ips=ips
    cat iplist | sh searchip.sh
    

    or

    sh searchip.sh < iplist  
    

    Then you get two files , one is ip found, other one is ip not found.
    What you need is shell I/O redirection.

    0 讨论(0)
  • 2020-12-22 07:59

    If you sort the two files, you can use the comm command:

    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
    

    will show the IP addresses that are not advertised, and:

    comm -12 all_ip_addresses_sorted advertised_ip_addresses_sorted
    

    will show the advertised IP addresses.

    You can also avoid creating the separate sorted files by using process substitution:

    comm -23 <(sort all_ip_addresses) <(sort advertised_ip_addresses)
    
    0 讨论(0)
  • 2020-12-22 08:04
    $ script < list_of_ip_addresses
    

    That's all you need.

    0 讨论(0)
提交回复
热议问题