Donate : Link
Medium Blog : Link
Applications : Link
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression. Its name comes from the ed command g/re/p (globally search for a regular expression and print matching lines), which has the same effect.
grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others.
~/codeFactory$ grep --help
Usage: grep [OPTION]... PATTERNS [FILE]...
Search for PATTERNS in each FILE.
Example: grep -i 'hello world' menu.h main.c
PATTERNS can contain multiple patterns separated by newlines.
Pattern selection and interpretation:
-E, --extended-regexp PATTERNS are extended regular expressions
-F, --fixed-strings PATTERNS are strings
-G, --basic-regexp PATTERNS are basic regular expressions
-P, --perl-regexp PATTERNS are Perl regular expressions
-e, --regexp=PATTERNS use PATTERNS for matching
-f, --file=FILE take PATTERNS from FILE
-i, --ignore-case ignore case distinctions in patterns and data
--no-ignore-case do not ignore case distinctions (default)
-w, --word-regexp match only whole words
-x, --line-regexp match only whole lines
-z, --null-data a data line ends in 0 byte, not newline
Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version display version information and exit
--help display this help text and exit
Output control:
-m, --max-count=NUM stop after NUM selected lines
-b, --byte-offset print the byte offset with output lines
-n, --line-number print line number with output lines
--line-buffered flush output on every line
-H, --with-filename print file name with output lines
-h, --no-filename suppress the file name prefix on output
--label=LABEL use LABEL as the standard input file name prefix
-o, --only-matching show only nonempty parts of lines that match
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE;
TYPE is 'binary', 'text', or 'without-match'
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories;
ACTION is 'read', 'recurse', or 'skip'
-D, --devices=ACTION how to handle devices, FIFOs and sockets;
ACTION is 'read' or 'skip'
-r, --recursive like --directories=recurse
-R, --dereference-recursive likewise, but follow all symlinks
--include=GLOB search only files that match GLOB (a file pattern)
--exclude=GLOB skip files that match GLOB
--exclude-from=FILE skip files that match any file pattern from FILE
--exclude-dir=GLOB skip directories that match GLOB
-L, --files-without-match print only names of FILEs with no selected lines
-l, --files-with-matches print only names of FILEs with selected lines
-c, --count print only a count of selected lines per FILE
-T, --initial-tab make tabs line up (if needed)
-Z, --null print 0 byte after FILE name
Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
-NUM same as --context=NUM
--color[=WHEN],
--colour[=WHEN] use markers to highlight the matching strings;
WHEN is 'always', 'never', or 'auto'
-U, --binary do not strip CR characters at EOL (MSDOS/Windows)
When FILE is '-', read standard input. With no FILE, read '.' if
recursive, '-' otherwise. With fewer than two FILEs, assume -h.
Exit status is 0 if any line (or file if -L) is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.
Report bugs to: bug-grep@gnu.org
GNU grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <https://www.gnu.org/gethelp/>
1. Case insensitive search: The -i option enables to search for a string case insensitively in the give file.
~/codeFactory$ cat test4.txt
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.
Its name comes from the ed command g/re/p (globally search for a regular expression and print matching lines), which has the same effect.
grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others.
~/codeFactory$ cat test4.txt | grep -i GREP
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.
grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others.
2. Displaying the count of number of matches: We can find the number of lines that matches the given string/pattern.
~/codeFactory$ grep -c "grep" test4.txt
2
3. Display the file names that matches the pattern: We can just display the files that contains the given string/pattern.
~/codeFactory$ grep -l "grep" test*
test4.txt
4. Checking for the whole words in a file: By default, grep matches the given string/pattern even if it found as a substring in a file. The -w option to grep makes it match only the whole words.
~/codeFactory$ grep -w "grep" test4.txt
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.
grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others.
5. Displaying only the matched pattern: By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option.
~/codeFactory$ grep -o "grep" test4.txt
grep
grep
6. Show line number while displaying the output using grep -n: To show the line number of file with the line matched.
~/codeFactory$ grep -n "grep" test4.txt
1:grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.
3:grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others.
7. Inverting the pattern match: You can display the lines that are not matched with the specified search sting pattern using the -v option.
~/codeFactory$ grep -v "grep" test4.txt
Its name comes from the ed command g/re/p (globally search for a regular expression and print matching lines), which has the same effect.
8. Matching the lines that start with a string: The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.
~/codeFactory$ grep "^grep" test4.txt
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.
grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others.
9. Matching the lines that end with a string: The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.
~/codeFactory$ grep "expression.$" test4.txt
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.
10. Specifies expression with -e option. Can use multiple times:
~/codeFactory$ grep -e "grep" -e "command" test4.txt
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.
Its name comes from the ed command g/re/p (globally search for a regular expression and print matching lines), which has the same effect.
grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others.
11. -f file option Takes patterns from file, one per line:
~/codeFactory$ cat grep.txt
grep
command
~/codeFactory$ grep -f grep.txt test4.txt
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.
Its name comes from the ed command g/re/p (globally search for a regular expression and print matching lines), which has the same effect.
grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others.
12. Print n specific lines from a file: -A prints the searched line and n lines after the result, -B prints the searched line and n lines before the result, and -C prints the searched line and n lines after and before the result.
Syntax:
$grep -A[NumberOfLines(n)] [search] [file]
$grep -B[NumberOfLines(n)] [search] [file]
$grep -C[NumberOfLines(n)] [search] [file]
Example:
~/codeFactory$ grep -A1 grep test4.txt
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.
Its name comes from the ed command g/re/p (globally search for a regular expression and print matching lines), which has the same effect.
grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others.
~/codeFactory$ grep -A0 grep test4.txt
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.
--
grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others.

