Basic filtering with grep

One of the commands I use the most in the terminal is grep, even more than cd o ls.

grep It has many options and offers dissimilar possibilities, however I use the most conventional way possible, but let's start by explaining What is grep?

grep is simply a filter, it is a command that shows lines that match the filter we have declared.

For example, in our system we have the file / usr / share / doc / bash / FAQ and the content of this file is:

View file content

If you want to list the content in the terminal with the command cat (yes cat, like cat hehe) they can do it:

cat /usr/share/doc/bash/FAQ

Now, suppose we only want to list the line of that file that talks about the version, for this we use grep:

cat /usr/share/doc/bash/FAQ | grep version

Putting that in the terminal will only show you the line that contains "version" in that file, it will no longer show any line that does not contain that word.

What if I want to show everything except the version line?

That is, in the way I explained to you, everything that matches the filter would be shown, now I will show you how to make everything appear except what matches the filter:

cat /usr/share/doc/bash/FAQ | grep -v version

Do you notice the difference? ... simply adding -v it already makes a difference 😀

So if they put grep it will only show you what matches the filter, but if you put grep -v it will show you everything except the filter.

Well here the post ends, just another tip that now maybe they can despise it but ... they have no idea how useful grep can be, it is seriously a life saver 😀


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

11 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *



  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   DMoZ said

    Undoubtedly a very versatile command, once you learn to handle it, it makes your life easier =) ...

  2.   Scalibur said

    Hi! .. ..really a very useful command .. in my case I use it quite a bit ..

    A simple example would be, for example, dpkg -l | grep 'package' (in case of distros based on debian), it is used to know if we have that package installed.

    Great to give these tools to our entire community 😉

    1.    KZKG ^ Gaara said

      Thank you very much
      Indeed, grep is as powerful as our imagination hahahaha, together with awk (and cut) they really achieve wonders * - *

      I will put a couple more tips for terminal work soon 😉
      Greetings and thanks for your comment.

      PS: Interesting your email LOL !!

  3.   hexborg said

    Very good!! Yes. Certainly grep is one of the life savers for anyone who likes to use the terminal. Just a couple of points: You really don't need to use the cat command at all. You can put the filename as a grep parameter like this:

    grep version / usr / share / doc / bash / FAQ

    Also, even if it couldn't, there would always be the option to redirect the command input by doing something like this:

    grep version </ usr / share / doc / bash / FAQ

    The latter can be done with any command, so it is never necessary to use cat to send a file to the input of a command.

    Using a redirect instead of cat causes the shell to launch one less process, thus consuming fewer resources. It is not an appreciable difference, but it is considered good practice.

    On the other hand, grep becomes really useful when using regular expressions ... If I wanted to help out by making a post about regular expressions, what would I do? Is it enough to add a new post from the wordpress desktop?

    1.    KZKG ^ Gaara said

      Oh interesting, I always got used to using it with cat HAHAHAHA, thanks for the tip 😀

      1.    Hugo said

        With grep you can also make filters a little less basic, for example:

        grep -B3 -A3 -E -i --color=auto -n "(desde|hacia)?linux(\.)?$" ~/miarchivo.txt

        This basically shows the lines that contain the term we are looking for (which can be in any combination of uppercase and lowercase), plus the three lines before and after the three, highlights the results in a different color, puts line numbers to the results, and allows the enabling of extended regular expressions that in this case allow searching in "myfile.txt" for all lines ending with desdelinux, hacialinux or linux just plain (with or without a full stop).

        By the way, regular expressions offer a lot of flexibility and every good "geek" with a passion for free software should learn to use them, hehe.

  4.   Dragnell said

    It is also possible to use zgrep for tablets in .ta.gz it is very useful when we want to review old logs. Cheers

  5.   jhon said

    Hi. thanks for the post. It happens to me that using grep, the word that I write in the lines that appear does not turn color. (generally it is like this) [example: grep cat file.txt]
    the lines and the cat appear, but the cat does not turn a certain color to distinguish it
    (in the ccompus of my uni it is seen)
    Do you know how I could activate this option?
    Please if you can answer me. my email is

    1.    fdy nb said

      friend has to write cat in quotation marks 'cat' or also "cat" followed by the name of the file where he wants to look for it

  6.   enrique said

    Hello friend, you are absolutely right, you have a great sense of usefulness. From now on, grep will put it first in my list of favorite commands.

  7.   scanjura said

    And how would it be to show employees filtered by salary?