Grep
Before it had a name, grep was a private tool written by Ken Thompson, inspired by a function in ed that he also made called g/re/p. Grep was built to search files for certain patterns and retrieve the matching lines.
grep [OPTION]... PATTERNS [FILE]
Grep is a command-line tool used to search text files or lines of text in a standard input stream by looking for patterns. grep is useful for searching for certain software installations, finding specific methods, and organizing large repositories. grep prints each line that matches the pattern.
Common Options:
Options |
Description |
|---|---|
-n |
Prints the line number of the found patterns. |
-r |
Recursive grep searches through an entire folder, including enclosed folders and files. |
-E |
Allows your grep to understand extended regular expressions. |
-e |
Can be chained before and between patterns to specify that one or more patterns can be used while searching. |
-v |
inverts the matching to select non-matching lines. |
Using grep to find a pattern
Because of grep’s processing speed, much of its power comes from using it in a pipe to process large amounts of information quickly. Say you are trying to find a specific process. You can combine grep with the ps command to streamline the process.
CS@Support:~/Grep_fun$ ps aux | grep 'vscode' | grep -v grep
CS 758 0.0 0.3 1011812 51180 pts/0 Sl+ 08:34 0:00 /home/CS/.vscode-remote-containers/bin/18e3a1ec544e6907be1e944a94c496e302073435/node
This command pipes the output of ps aux to a grep search for ‘vscode’, returning only the lines that match our pattern.
Then, for an additional layer of quality assurance, we pipe that output to another grep call, omitting any lines that include the word grep, because while returning processes, grep would also find processes of grep searching for our pattern.
CS@Support:~/Grep_fun$ ps aux | grep 'vscode'
CS 758 0.0 0.3 1011812 51180 pts/0 Sl+ 08:34 0:00 /home/CS/.vscode-remote-containers/bin/18e3a1ec544e6907be1e944a94c496e302073435/node
CS 1407 0.0 0.0 6520 1920 pts/2 S+ 08:57 0:00 grep vscode
Using grep to omit a pattern.
When parsing documents or code, there is often a large amount of information that can be distracting from what you are trying to understand. Say you’re trying to understand a section of code, but it is absolutely littered with comments.
#include <stdio.h>
//including stdio.h allows us access to a variety of functions for input, output, and file handling.
int main() {
// We need to return a 0 on a successful run which is every run in this program
// Printf defaults to stdout
printf("hello world\n");
// Fprintf allows us to say what output stream we want our text to be printed to.
// in the example below we are printing to our standard error
fprintf(stderr, "hello world\n");
// Returning 0 to get rid of potential error
return 0;
}
Instead of reading this distracting product, we could use the grep command to get a cleaner output of our code.
grep -v "//" hello_world.c
CS@Support:~/Grep_fun$ grep -v "//" hello_world.c
#include <stdio.h>
int main() {
printf("hello world\n");
fprintf(stderr, "hello world\n");
return 0;
}
Using grep to search recursively
grep’s -r option is one of its most widely used features; with this flag, we can search an entire file tree for a specific line. Let’s say we are trying to find all instances of the phrase apt install because we want to switch our scripts from Linux to macOS. The command below lets us search files below our current directory for occurrences of our desired pattern.
CS@Support:~/tree$ grep -r "apt install" $(pwd)
/home/CS/tree/scripting/README.md:Script to apt install aafire and htop in computers that do not have them downloaded
/home/CS/tree/scripting/README.md:apt install can be replaced by Brew install for macos machines
/home/CS/tree/scripting/script.sh:apt install aafire
/home/CS/tree/scripting/script.sh:apt install htop
/home/CS/tree/hello:apt install.
More information
Grep is a basic tool in the Linux command line that you can use every day to speed up your workflow. Getting accustomed to it will not only increase your productivity but also deepen your understanding of regular expressions and how the Linux file system works. More information about regular expressions can be found at Regular Expressions for Beginners
Please consult the man pages for more details about grep.