简体   繁体   中英

How to grep for lines which contain particular words in a log file?

I have a big log file which I am trying to scan it for a particular words. In general, I will have few words which I need to grep on my big log file and print out the line which contains those words.

I know how to do simple grep on a file. Suppose if my file name is abc.log and I need to find a line which contains word "hello" then I always do it like this and it prints out the line for me.

grep -i "hello" abc.log

But I don't know how to do the grep for combination of words. Meaning I would have list of words and I will scan my abc.log file for all those words and I will print out the lines which contains those words individually.

#!/bin/bash

data="hello,world,tester"

# find all the lines which contains word hello or world or tester

So in my above shell script I will split my data variable and look for hello word in abc.log so any line which contains hello word, I will print it out and similarly with world and tester as well.

I am trying to make this pretty generic so that I just need to add my list of words in the data variable without touching the actual logic of grepping the logs.

我会使用正则表达式,如下所示:

grep -E 'hello|world|tester' abc.log

If you store your patterns in a file, one per line, you can use grep -f file-with-patterns file-to-search.log

From the man page:

   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)

Edit 2018:

Since I wrote this, I have become aware of the following interesting edge cases:

  • You can read the list of patterns from pipe using -f - (if you don't need stdin, ie you specified files on grep's command line) or -f <() (in any case)
  • grep 's performance starts to fail badly if hundreds of patterns are passed. If your use case is that insane, consider generating and immediately executing a sed (or some other language) script, although this could potentially have problems with overlapping patterns.

Apart from bruchowski's answer , you can also use:

grep -i -e "hello" -e "world" -e "tester" abc.log

OR

grep 'hello\|world\|tester' abc.log

OR

egrep 'hello|world|tester' abc.log

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