Q:

linux grep

grep -R "keyword" /directory/
4
# EXAMPLE 1: look for any files (with names ending in ".c++") for the text "::MethodA("
grep "::MethodA(" *.c++

# EXAMPLE 2: display only the matching file names (not the row too) of the matches
grep -l "MethodA(" *.c++

# SYNTAX
# grep [optional-filters] "<your-string-to-search>" <files-to-search>


# OPTIONAL-FILTERS
# +--------+----------------------------------------------------------------------------+
# | OPTION |  DESCRIPTION                                                               |
# +--------+----------------------------------------------------------------------------+
# |  -e    |  pattern                                                                   |
# |  -i    |  Ignore uppercase vs. lowercase.                                           |
# |  -v    |  Invert match.                                                             |
# |  -c    |  Output count of matching lines only.                                      |
# |  -l    |  Output matching files only.                                               |
# |  -n    |  Precede each matching line with a line number.                            |
# |  -b    |  A historical curiosity: precede each matching line with a block number.   |
# |  -h    |  Output matching lines without preceding them by file names.               |
# |  -s    |  Suppress error messages about nonexistent or unreadable files.            |
# |  -x    |                                                                            |
# |  -f    |  file: Take regexes from a file.                                           |
# |  -o    |  Output the matched parts of a matching line.                              |
# +--------+----------------------------------------------------------------------------+
2
grep 'word' filename
fgrep 'word-to-search' file.txt
grep 'word' file1 file2 file3
grep 'string1 string2'  filename
cat otherfile | grep 'something'
command | grep 'something'
command option1 | grep 'data'
grep --color 'data' fileName
grep [-options] pattern filename
fgrep [-options] words file
4
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
 with each such part on a separate output line.
2

New to Communities?

Join the community