A utility to search content input that match with one or more RegEx patterns. The content input can be provided by either passing a file path or from a standard input (stdin).
General syntax for
$ grep [OPTIONS...] [PATTERN] [FILE...]
Search for something within a file
grep exp FileName.txt
grep is a powerful command that allows you to search for a specific set of characters, or words exist in a file, or multiple files. The command above search for
FileName.txt, and return results when found.
grep is by default case-sensitive, and without other parameters involved,
grep would return results as long as it matches “exp”.
- “exp” = true
- “Exp” = false
- “exponential” = true
- “Expensive” = false
Search for something in multiple files
grep all name1.txt name2.txt name3.txt
This command expands searching to multilple specified filenames. The example command seach for the word "all" in
Finding an exact word with
grep -w example Example.txt
grep gets more precise in its search and only return true if the exact word matches. In the command above,
grep search for "example" in
Any of the following would return false:
Case-insensitive search with
grep -i being ExampleFile.txt
grep will search in a case-insensitive manner and will return true as long the input matches, regardles if it’s lowercase or uppercase characters.
The command above searches for the word "being" in
ExampleFile.txt, and will return result if found.
All the following will return true with existence of
Count and output word repeatation with
grep -c smallness TextFile.txt
grep will first find if a specific word exist, and then count how many times it’s being repeated. The command above search for "smallness" and return the number of times it existed in
Inverse search with
grep -v lorem sometext.txt
-v excludes the entire line that matches the input pattern, and output the rest that doesn’t contain it. The command above searches for "lorem" in
sometext.txt. Any lines without "lorem" will return true.
Display matching line and list line number
grep -n ipsum randomtext.txt
-n returns content with line-count. When a search word is included, it returns the entire line (where word exists) with its line-count. The command above search for "ipsum" in
randomtext.txt, and its output shows which line "ipsum" is at.
List filenames that contain matched string
grep -l dolor *txt
-l parameter, only
.txt extension files that contain the word "dolor" will return true. Filenames will be printed instead of the entire lioe.
Search lines starting with a pattern
grep ^Example TextFile.txt
^ in front of a search-pattern suggests
grep should only look words that starts with the search-pattern and nothing else. The command above will search in
TextFile.txt, and return all lines that begins with "Example".
Multiple pattern search with
grep -e lorem -e amet ExampleFile.txt
-e parameter can be used multiple times in the same command; each paired with a search-pattern, allows you to be more specific in searching for something in a file. The command above searches for the words "lorem", and "amet" in
ExampleFile.txt, and return if true/found.