Home | Login and Logout | Basic Command Line Commands | File System | File Editing Through Terminal | Java Programs | Input/Output Redirection | Grep | Works Cited

What is Grep?

The term "grep" stands for "global regular expression print". It processes files line by line and prints the lines that match a specified pattern.

Using Grep to Process Specific Files

Grep Syntax
To use grep for processing specific files, you must follow the following format:

grep [Options] [StringPattern] [File]

Options
[Options] is the options we want to want to apply to our search. Multiple options can be used in a single grep command. To apply options to a grep command, replace [Options] with the desired options you wish to apply. For example, to ignore case ("-i") and print the line number of the matching pattern ("-n"), we would replace [Options] with "-i -n". A list of options will be displayed further into this page.

StringPattern
[StringPattern] is the pattern that you want to detect. The grep command will analyze each line of the specified file for the pattern specified by [StringPattern]. To search for a specific pattern, replace [StringPattern] with the desired pattern. For example; to search for all instance of a "calculator" pattern, replace [StringPattern] with "calculator". Regular Expressions can also be used for the pattern, which will be discussed further into this page.

File
[File] is the file that you want to analyze. If desired, you can specify multiple files that you wish to analyze. For example: if you want to analyze a java file called "HelloWorld.java" and another one called "Program.java", you would enter "HelloWorld.java Program.java" for [File]. Before using grep, make sure that you are in the directory that contains the file in the terminal.

Additionally, you can also recursively search subdirectories by using the "-r" option in [Options], and the subdirectory or a simple "*" in the [File] part of the command. This will search through all files and subdirectory in the subdirectory specified, or the current directory if the "*" is used.

Example of Using Grep On One File

grep one file search

Example of Recursive Grep

recursive grep

Using Regular Expressions

What makes grep really useful is the fact that we can use regular expressions to search our files. For example, if we want to search for phrases similar to "my friend", we can replace [StringPattern] with "my.*friend". The "." and "*" are regex terms, with the "." representing a single wildcard character, and the "*" meaning that any number of the preceding character, 0 or more times, will match. So ".*" means that patterns such as "my friend", "myfriend", "my best friend", "my girlfriend", and "my ultra-cool teenage friend" will match. We can further combine this with the "-i" [Option] to ignore the case of any of these phrases. This means patterns such as "mY FriEND", "mYFriENd", "My Best Friend", and My aunt's friend" will also match.

More regular gxpressions aside from "." and "*" can be used in grep commands. Regular expressions are extremely powerful tools that can help search for a variety of patterns. Here is a table of some of the regular expressions we can use in [StringPattern]:

Regular Expression Description
^ Match expression at the start of a line. Example: "^Hello"
$ Match expression at the end of a line. Example: "bye$"
\ Deactivate the special meaning of the next character. Example: "\."
[ ] Match any of the enclosed characters such as [abcd]. Use Hyphen "-" for a number range, such as [0-9]
. (Period) Match any single character (except if at end of pattern). Example: "A Honda Civic-. Car"
* Match zero or more of the preceding character. Example: "NO*!"

Grep Options

Grep has quite a lot of useful options ready to be used. Multiple of these can be used in the [Options] part of the grep command. Here is a table of some Grep Options:

Option Description
-i Ignores the case of characters in the [StringPattern].
-n Displays the line number of the matching pattern.
-r Recursively search subdirectories. Goes through all files in a subdirectory.
-c Display the number of matched lines (Counts number of times there is a match).
-l Display the filenames, but not the lines where there is a match
-v Display all the lines that don't match the pattern

(Green, 2017; Linux grep command; UNIX Basic commands)



Written by: Daniel Amusin, Derek DeCramer, Nikhil Thammadi