Example of using grep
searching for the word "modules":
grep -r "modules" .
By using the "-r" switch, we're telling grep to scan files in the current directory and all sub-directories. It will return a list of files the string was found in, and a copy of the line it was found on.
To just return the file name with out the line that matched:
grep -lr "modules" .
Use the grep to find strings match a regular expressions,
grep -lr "mod.*" .
That command will print a list of files containing any word starting with "mod".
to search for multiple words:
grep -r "drupal\|joomla\|wordpress" .
Also to search for multiple patterns
# Interview question on grep:
A often asked question is to use grep to find the phone numbers in a very large file:
http://www.panix.com/~elflord/unix/grep.html
Suppose you want to match a specific number of repetitions of a pattern. A good example is phone numbers.
You could search for a 7 digit phone number like this: grep "[[:digit:]]\{3\}[ -]\?[[:digit:]]\{4\}" fil
grep options search_string file...
http://www.macworld.com/article/41504/2004/12/jangeekfactor.html
The correct solution is to avoid regular expressions and instead use fixed strings (-F) that must match an entire line (-x).
grep -Fxf file1 file2
No comments:
Post a Comment