DEV Community

Amanda Igwe
Amanda Igwe

Posted on

2 1 1 1 1

Day 17/ 30 Days of Linux Mastery: Grep Command

Table of Contents


Introduction

Welcome back to day 17!. Today, we are talking about a core command in Linux, the grep command.

When I think of grep I simply think of a filter.

If you have ever needed to search for a word inside a file, filter logs, or analyze output from a command, grep is your go-to tool.

Let’s get into it!


What is grep?

grep stands for Global Regular Expression Print. It searches for lines in a file or input that match a given pattern and prints them out.

You can use it to:

  • Search through config files
  • Analyze logs
  • Combine it with other commands to extract exactly what you need

Core grep Commands

Before we list the core grep commands, here is the basic syntax;

grep [options] pattern [file...]
Enter fullscreen mode Exit fullscreen mode

Here are the grep commands and their use;

grep Command Description
-i Case-insensitive search
-v Invert match (show lines that do not match)
-r or -R Recursive search through directories
-n Show line numbers of matches
-c Count the number of matching lines
-l List only the filenames that contain the match
-e Use multiple patterns
--color=auto Highlight the match in color for readability

Real-World Scenario: Using grep Commands

  • Let's say we have an error log file.

g1 description

  • We decide to filter the file and select only the lines with ERROR
grep "ERROR" demologs.txt
Enter fullscreen mode Exit fullscreen mode

g2 description

  • Show line numbers of the errors found
grep -n "ERROR" demologs.txt
Enter fullscreen mode Exit fullscreen mode

g3 description

  • Let's imagine there was a word 'error', but not in uppercase format. We want to filter for errors but make it case-insensitive.
grep -i "error" demologs.txt
Enter fullscreen mode Exit fullscreen mode

g4 description

  • Exclude Certain Matches
grep -v "ERROR" demologs.txt - # Case-Sensitive
grep -iv "error" demologs.txt  - # Case-Insensitive
Enter fullscreen mode Exit fullscreen mode

g6 description

Note:
grep -v "error"
This excludes all lines that contain the exact string "error" (case-sensitive).
So, it will only remove lines that match "error" in lowercase. It will still show lines like "Error" or "ERROR".

grep -iv "error"
This adds the -i flag, which makes the search case-insensitive, so it excludes lines containing error, Error, ERROR, or any variation in letter case.
The result is cleaner if you are trying to remove all forms of "error" regardless of case.


Conclusion

This is a job-ready skill. As a Linux or DevOps engineer, being able to search logs, filter noise, and pinpoint issues quickly with grep can save valuable time in production environments.

If this is helpful to you, feel free to bookmark, comment, like and follow me for Day 18!


Let's Connect!

If you want to connect or share your journey, feel free to reach out on LinkedIn.
I am always happy to learn and build with others in the tech space.

#30DaysLinuxChallenge #Redhat#RHCSA #RHCE #CloudWhistler #Linux #Rhel #Ansible #Vim #CloudComputing #DevOps #LinuxAutomation #IaC #SysAdmin#CloudEngineer

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Postmark Image

"Please fix this..."

Focus on creating stellar experiences without email headaches. Postmark's reliable API and detailed analytics make your transactional emails as polished as your product.

Start free

👋 Kindness is contagious

Dive into this informative piece, backed by our vibrant DEV Community

Whether you’re a novice or a pro, your perspective enriches our collective insight.

A simple “thank you” can lift someone’s spirits—share your gratitude in the comments!

On DEV, the power of shared knowledge paves a smoother path and tightens our community ties. Found value here? A quick thanks to the author makes a big impact.

Okay