grep: A DevOps and Cloud Engineer's Helper to Text Search

So here comes the grep (aka global regular expression print) the powerful command line tool that searches for patterns in text files. It's an essential tool for DevOps and Cloud Engineers, as it helps you to:

  • Search for specific text patterns in files, including regular expressions

  • Filter output to show only relevant results

  • Use various options to customize the search, such as ignoring case or searching recursively

Here's how you can use it to search for a specific text string within files.

Basic Syntax of grep Command

grep [options] pattern [files or file path]

In the terminal, you can type the following command :

$ grep -r "string" /path/to/file

Let's breakdown this code,

  • grep : The command used for searching text.

  • -r or --recursive : Recursively search in sub directories.

  • "string" : The text string you are searching for. Enclose it in quotes if it contains spaces or special characters.

  • /path/to/file : The path where you want to start the search.

Example Usage

Let's search for a Specific String in the Current Directory,

$ grep -r "string_text" .

This command will search for the string “string_text” in all files and sub directories within the current directory. In here " . " notation at the command end, which refers to the current working directory.

Let's search for a Specific String in a Specific Directory

$ grep -r "string_text" var/www/vhosts/

This command will search for the string “string_text” in all files and sub directories within var/www/vhosts/.

Additional Parameters

-i : Ignore case sensitive search

$ grep -ri "string_text" .

-l : Only print the names of files that matches the string.

$ grep -rl "string_text" .

-n : Show the line numbers where the string appears.

$ grep -rn "string_text" .

-w : Search for the exact string matches in files.

$ grep -rw "string_text" .

--exclude : Exclude files matching a pattern.

$ grep -r "string_text" . --exclude="*.txt"

--include : Only search files matching a pattern.

$ grep -r "string_text" . --include="*.txt"

For further learning, you can explore additional Grep command options and usage by using the following syntax :

$ grep --help

Or which can be found here: grep : Full Manual - gnu.org

Let's see better example with some additional options,

For the string "string_text" in all .log files within the /home/mobaxterm/sample directory, ignoring case, and display the line numbers.

$ grep -rin "string_text" /home/mobaxterm/sample --include="*.log"

In this article, I've covered the basics of grep and explored some of its most useful options. With this knowledge, you're now equipped to start using grep to simplify your work and improve your productivity.

Thank you for reading my blog post! Your positive feedback inspires me to explore more about technology and innovation. Happy grepping!"

Make sure to follow me on LinkedIn and pamindatechtalk where I will be posting similar content in the future! 🙋‍♂️

0
Subscribe to my newsletter

Read articles from Paminda Kalpa Wickramasinghe directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Paminda Kalpa Wickramasinghe
Paminda Kalpa Wickramasinghe

Hi folks ✌, I’m Paminda Kalpa Wickramasinghe from the beautiful city Galle, a charming coastal city in southern Sri Lanka 🌊. I hold a Bachelor’s degree in Information Technology from the University of Colombo School of Computing. With experience as an IT support professional in server management, backup solutions, and LAN administration, I'm passionate about transitioning to in demand fields like DevOps, Cloud Computing, Platform Engineering, Site Reliability Engineering and emerging technologies.