📖
Resources
  • 👋Welcome!
  • Semesters
    • 🚀S1 & S2
      • 🔍Focus
      • âš—ī¸Chemistry
      • 🔭Physics
      • 📊LAC
      • 📈VCD
      • đŸ˜ī¸BCE - BME
      • 🔌BEE
      • 📐Graphics
      • âš™ī¸Mechanics
      • đŸ–Ĩī¸CP
      • â›ˇī¸Life Skills
      • 🧑‍đŸ’ŧPC
    • 📚S3
      • 📉DS
      • âš™ī¸DE
      • 📊DMS
      • 🧑‍đŸ’ģOOP
      • đŸ–Ĩī¸OOP Lab
      • đŸ—ī¸LSD
      • 🌱SE
      • 🔍Extras
    • 📓S4
      • 📊GT
      • 💾COA
      • đŸĒŖDBMS
      • đŸ’ŊOS
      • 😃PE
      • đŸ‡ŽđŸ‡ŗCOI
      • đŸĨŧDE Lab
      • đŸ’ģOS Lab
      • 🔐Number Theory
    • âœī¸S5
      • 💾FLAT
      • 🛜CN
      • đŸ’ŊSS
      • đŸ•šī¸MM
      • đŸ’ģMOSS
      • â˜ĸī¸DM
      • đŸĨŧSS&M Lab
      • đŸĒŖDBMS Lab
      • Cryptographic Algorithms
    • 📖S6
      • CD
      • CGIP
      • AAD
      • PIP (Elective)
      • IEFT
      • CCW
      • Networking Lab
      • Mini Project
    • 📝S7
    • 🎓S8
  • Resources
    • Git
      • Git installation
      • What is Git?
    • Linux
      • Introduction
        • History of Operating System
        • Rise of GNU Project
        • Basic Terminologies
      • Linux shell
      • Commands
        • The Linux man command
        • The Linux ls command
        • cd command
        • The Linux pwd command
        • The Linux mkdir command
        • The Linux rmdir command
        • mv command
        • cp command
        • open command
        • touch command
        • find command
        • Cat command
        • grep command
        • Echo command
        • Vim Editor
        • Emacs Editor
        • Nano editor
        • whoami command
        • who command
        • su command
        • sudo command
        • passwd command
        • clear command
        • History command
    • DSA
      • 1.2 Concepts of Sliding Window Technique
      • 2.2 Characteristics of Sliding Window Problems
      • 1.3 Introduction to Deque and Monotonic Stack
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Resources
  2. Linux
  3. Commands

grep command

PreviousCat commandNextEcho command

Last updated 1 year ago

Was this helpful?

The Linux grep command

The grep command is a very useful tool. When you master it, it will help you tremendously in your day to day coding.

If you're wondering, grep stands for global regular expression print.

You can use grep to search in files, or combine it with pipes to filter the output of another command.

For example here's how we can find the occurences of the document.getElementById line in the index.md file:

grep -n document.getElementById index.md
Screen-Shot-2020-09-04-at-09.42.10

Using the -n option it will show the line numbers:

grep -n document.getElementById index.md

One very useful thing is to tell grep to print 2 lines before and 2 lines after the matched line to give you more context. That's done using the -C option, which accepts a number of lines:

grep -nC 2 document.getElementById index.md

Search is case sensitive by default. Use the -i flag to make it insensitive.

As mentioned, you can use grep to filter the output of another command. We can replicate the same functionality as above using:

less index.md | grep -n document.getElementById

The search string can be a regular expression, and this makes grep very powerful.

Another thing you might find very useful is to invert the result, excluding the lines that match a particular string, using the -v option:

Screen-Shot-2020-09-04-at-09.47.04
Screen-Shot-2020-09-04-at-09.44.35
Screen-Shot-2020-09-04-at-09.43.15
Screen-Shot-2020-09-04-at-09.42.04