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.

How to create a new file from existing file using grep command in unix.

Consider i have a file named as main.log having following contents:
Today is Monday
Today is Monday
Today is Tuesday
Today is Monday
Today is Wednesday
Today is Monday
Today is Thursday

And now i want to make a new file which contains line having “Monday” .
Command is :
grep “Monday” main.log > monday.log

Now if in case you want to append to the same file then:
grep “Monday” main.log >> monday.log

The single > is used to create a new file and >> is used to append the existing file.