简体   繁体   中英

Using Bash to search files for

I have two text files, 1 with a unique list of IP addresses and the other with a longer list of an IP address population. I am trying to identify what IP addresses from the unique list appear in the longer population list using bash.

So far I have:

#!/bin/bash
    while read line; do
        grep -Ff "$line" population.txt
done < $uniqueIP.txt

The script runs but I get no output. I am trying to count the number of occurrences but having issues coming up with the logic. Currently, the script does not output anything.

Sample Input:

uniqueIP.txt

192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14

population.txt

192.168.1.12
192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.17
192.168.1.18
192.168.1.19
192.168.1.22
192.168.1.23

Sample Output:

Found: 192.168.1.12
Found: 192.168.1.14
Total: 2

Here is my suggested bash function count_unique_ips for your problem

#!/bin/bash

function count_unique_ips()
{
  local unique_ips_file=$1
  local population_file=$2

  local unique_ip_list=$(tr ' ' '\n' < ${unique_ips_file} | sort | uniq)

  for ip in "${unique_ip_list[@]}"; do
    count=$(grep -o -i ${ip} ${population_file} | wc -l)
    echo "'${ip}': ${count}"
  done
}

count_unique_ips uniqueIP.txt population.txt

You should get the following output based on 2 given input files uniqueIP.txt and population.txt

'192.168.1.10':        0
'192.168.1.11':        0
'192.168.1.12':        1
'192.168.1.13':        0
'192.168.1.14':        1

A short method, using only grep , could be

grep -Fxf uniqueIP.txt population.txt

but this would be slow for large files. A faster way is

sort uniqueIP.txt population.txt | uniq -d

assuming the IPs in population.txt are also unique within this file, and one IP per line without extra characters (including blank characters).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM