How do I use the grep

grep Linux tutorial with examples

The grep command Many Linux and Unix users already know them, but often only superficially. Most don't use it for anything other than simple string searches. If for you grep not more than grep "hello" file means or you don't even know grep, you should read this tutorial 🙂

grep

grep is a Unix command with which you can search lines from code and log files or command outputs for strings. For example: show me all lines from file xyz.txt that contain the word “hello”.

grep stands for Global regular expression print. That means that you too Regular expressions can use for the search. If you don't already know what regular expressions are, you should check out a regex tutorial. For example, the interactive one from regexone.com is recommended. Without regexes is grep but of course also usable.

egrep or grep -E?

egrep is like grep, with the difference that you also enter +,?, | and () can be used as a regular expression. egrep is nothing more than a. In fact, on many machines, egrep is just a link to grep. In addition, egrep is deprecated, which is why you should reach for it.

grep options:

  1. Spellings (short & long)
  2. grep regex syntax
  3. grep recursive / subfolder
  4. Show NOT affected lines
  5. Ignore upper and lower case
  6. Find whole words only

Customize grep output

  1. Show number of hits
  2. Line number for each hit
  3. Show only filenames instead of affected lines
  4. Only show filenames that are NOT affected
  5. Color hits (also by default)
  6. Load search string from file

Test file

All example outputs refer to the following file file.txt:

You can simply send the file via

create.

grep command: syntax

The basic syntax of a grepCall in the shell is as follows:

annotation: Shell parameters in [] such as [Options] are optional, which means that you can leave them out if you like.

grep options

You can use various options to control the behavior and output of grep change.

Spellings

There are two ways of specifying the options. Most Linux commands have these variants:

  1. the short form, a minus sign and immediately after that the option letter. E.g:

    Advantages: shorter, and with this shape you can also clamp several options one behind the other, e.g.

  2. the long form, two minus signs and the option name written out. E.g:

grep regex syntax

Do you want +,?, | and () in the search pattern, you have to append the parameter -E.

searches for “halo” and “hello” in the file.txt.

Output:

grep recursive: search subdirectories

If you also want to search files in the subdirectories, you must also specify the option.

Show NOT affected lines

If you only want to see lines that do NOT contain the search string, this option will help you.

Example:

Output:

Ignore upper and lower case

By default, the grep commandcase-sensitive. That means, upper and lower case letters are considered. Do you want without this, well case-insensitive, you need to add the option.

Example:

Output:

Find whole words only

Finds by default grep all occurrences of the search string, both in parts of words and in whole words. You only limit it to occurrences in whole words via.

Example:

Output:

Customize grep output

Show number of hits

You can display the number of all lines that contain the search pattern with or. Grep returns the pure number, e.g. “4” for 4 hits, nothing else.

Example:

Output:

Line number for each match

This option is particularly useful for source code files. If you pass this option to the grep command, the line number of the match is written before each match.

Example:

Output:

Only output file names

Sometimes you just need the file name (s) in which the search string was found. You can do this via the option (small “L”).

Example:

Output:

Show found, NOT matching file names

There are also use cases in which you only want to have the file names listed in which the search string was NOT found. You use the option for this.

Example:

Output:

Color hits

The grep output is colorless by default. For clarity, it is helpful if you highlight the matched pattern in color in each line.

I have this in my or by default. If you want this too, all you have to do is write the line

in your shell config file. If you don't like the color, you can send it by

change at will. For a different color, simply replace one of the color codes in the quotation marks.

Color codes:

source

Load pattern from file

If you want to use more complex search patterns or regexes again and again, you can save them in files and include them in the grep command via.

Example: filter all lines with email addresses from a file

Write the regex to a file without the single quotes:

Now you can replace the regex in the example above with the file:

Tip: Make it a habit to always save all the regexes you use under a meaningful file name in a specific directory. Over time you will collect a comfortable, reusable regex compendium.

Examples

Here are a few practical examples for the use of grep:

Output graphics card

Regex: Find lines with “foobar” at the beginning (in file1.txt AND file2.txt)

Regex: find blank lines

find lines with “a”, but only as a whole word -> not “leg” or “move in”

Regex: Show all HDDs

List all mysql processes

Conclusion

grep offers many options such as regexes, with which it becomes really powerful.

Is something still unclear? Are you missing something in this article? Help me improve it, and please leave a comment or rate it below!