How to provide grep a file with ip addresses to look for in access.log


I have a file where each line has an IP address and I want to see if these Ip’s are found in access.log

File name: IpAddressess

Contents example :
etc etc

Now I want to scan access.log for these IP addresses contained in the file IpAddressess

Can I use the command grep for this and what would the command structure look like?

Thank you kindly for any assistance!

Asked By: user3077601


Yes, use grep -f. Worth adding the -F to that the . character in your patterns doesn’t get interpreted as ‘any’ character.

-f and -F is explained below

$ grep --help 2>&1|grep -i '^  -f'
  -F, --fixed-strings       PATTERNS are strings
  -f, --file=FILE           take PATTERNS from FILE


$ cat patterns
$ cat myaccesslog
world !
foobar 192.168.0x1
$ grep -f patterns myaccesslog
world !
foobar 192.168.0x1
$ grep -Ff patterns myaccesslog
world !
Answered By: steve

You are looking for the -f option, described in man grep on my Arch Linux system as:

   -f FILE, --file=FILE
          Obtain patterns from FILE, one per line.   If  this  option  is
          used  multiple  times  or  is  combined  with the -e (--regexp)
          option, search for all patterns given.  The empty file contains
          zero patterns, and therefore matches nothing.

However, since grep works with regular expressions and . in regular expressions means "any character", you will also want the -F option, so that 1.2.3 doesn’t match things like 10293:

   -F, --fixed-strings
          Interpret PATTERNS as fixed strings, not regular expressions.

Putting the two together, the command you’re looking for is:

grep -Ff IpAddressess access.log
Answered By: terdon
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.