0

How do I search in a textfile with grep for the occurrence of a word or another word?

I want to filter the apache log file for all lines including "bot" or "spider"

cat /var/log/apache2/access.log|grep -i spider

shows only the lines including "spider", but how do I add "bot"?

0

4 Answers 4

5

use classic regex:

grep -i 'spider\|bot'

or extended regex (or even perl regex -P):

grep -Ei 'spider|bot'

or multiple literal patterns (faster than a regular expression):

grep -Fi -e 'spider' -e 'bot'
1
  • +1 for covering multiple bases and also (implicitly) losing the cat.
    – tripleee
    Jun 26, 2013 at 8:54
1
cat /var/log/apache2/access.log | grep -E 'spider|bot'

With the -E option you activate extended regular expression, where you can use | for an logical OR.

Besides, instead of invoking another process - cat - you can do this with

grep -E 'spider|bot' /var/log/apache2/access.log
0

$ cat /var/log/apache2/access.log|grep -i 'spider\|bot'

The above will do the job.

You can also use egrep

$ cat /var/log/apache2/access.log|egrep -i 'spider|bot'

egrep is extended grep (grep -E). You will not have to use \ before | if you use egrep.

-1

You can use egrep instead:

cat /var/log/apache2/access.log|egrep -i 'spider|bot'
4
  • 3
    egrep is the same as grep -E. … Direct invocation as … egrep … is deprecated” – man grep
    – manatwork
    Jun 26, 2013 at 8:35
  • 2
    -1 The double pipe || means 'or', it's not a pipe at all. Also you might want to fix the UUCA.
    – tripleee
    Jun 26, 2013 at 8:52
  • The double-pipe ignored, on some platforms grep -E is not available and egrep is the only option.
    – Johan
    Jun 26, 2013 at 9:29
  • the double | was a mistype actually.
    – BitsOfNix
    Jun 26, 2013 at 10:04

Not the answer you're looking for? Browse other questions tagged .