← linux commands
Views:
grep command
The grep command is used to search a file for specific text.
Syntax: grep [options] pattern file
Example: The following command will search for occurrence of databaseURI in config.js file.
grep databaseURI config.js

Options:
-A
Prints n lines after the pattern match.
Example:
Let's consider an example where we have a huge file and we want to search for some function implementation. We can search for that function and print some line after that to check that.
grep -A 5 getTitleFromSlug config.js

-B
Prints n lines before the pattern match.
Example:
grep -B 8 getTitleFromSlug config.js

-C
Prints n lines before and after for every match and place a line (--) between two matches.
grep -C 1 console.log main.js

Example: Searching for regex pattern.
Suppose we have a list of emails and we want to find out emails ending with gmail.com
grep "@gmail.com$" emails.txt

Example: Usage with cat command along multiple files
Suppose we have multiple environment configuration files and we want to find some particular key from all of those environment config files.
cat *.config.js | grep someKey1
