Power of grep: search set of strings from an input file


grep is a common command which is used for search patterns in a file. I use it commonly for searching access log files, debug log files, looking for a pattern in user behavior.

If a user wants to search for a word or phrase in a file following is the command

grep "hello world" input.txt

this will list the lines which have the “hello world” in it.

grep works with regex expressions and support wild characters too.

grep "hello*" input.txt

will list all the lines having hello* pattern.

Now suppose there is a file with the list of user ids and the task is to search all the users who have visited the website from debug.log.

grep  -F -f userids.txt debug.log
The above command tells grep to look for strings as patterns from the input file

-F, –fixed-strings

  Interpret pattern as a set of fixed strings (i.e. force grep to behave as fgrep).

-f file, –file=file

 Read one or more newline separated patterns from a file.  Empty pattern lines match every input line.  Newlines are not considered part of a pattern.  If a file is empty, nothing is matched.

Advertisements