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"?
4 Answers 4
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 for covering multiple bases and also (implicitly) losing the
cat
.tripleee– tripleee2013年06月26日 08:54:11 +00:00Commented Jun 26, 2013 at 8:54
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
$ 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.
You can use egrep instead:
cat /var/log/apache2/access.log|egrep -i 'spider|bot'
-
3"
egrep
is the same asgrep -E
. ... Direct invocation as ...egrep
... is deprecated" – mangrep
manatwork– manatwork2013年06月26日 08:35:24 +00:00Commented Jun 26, 2013 at 8:35 -
2
-
The double-pipe ignored, on some platforms grep -E is not available and egrep is the only option.Johan– Johan2013年06月26日 09:29:20 +00:00Commented Jun 26, 2013 at 9:29
-
the double | was a mistype actually.BitsOfNix– BitsOfNix2013年06月26日 10:04:17 +00:00Commented Jun 26, 2013 at 10:04
Explore related questions
See similar questions with these tags.