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 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.
- Spellings (short & long)
- grep regex syntax
- grep recursive / subfolder
- Show NOT affected lines
- Ignore upper and lower case
- Find whole words only
Customize grep output
- Show number of hits
- Line number for each hit
- Show only filenames instead of affected lines
- Only show filenames that are NOT affected
- Color hits (also by default)
- Load search string from file
All example outputs refer to the following file file.txt:
You can simply send the file via
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.
You can use various options to control the behavior and output of grep change.
There are two ways of specifying the options. Most Linux commands have these variants:
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.
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.
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.
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.
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.
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.
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.
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”).
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.
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.
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.
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
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!
- Why do most Americans drink black coffee
- How is the air pressure used
- The American public is tired of scandal
- Why did the Vikings not use cavalry?
- How do I get to know autonomous cars
- Why do people check their IQ
- Are stethoscopes out of date
- Who is a pansexual
- What is the best university administration software
- What kind of fuel did Cassini use
- Why are buses always late in Yorkshire?
- What does psychology say about the use of medicine
- What happened to America's moral highness
- Where is the tomb of Tutankhamun
- What is the 3rd form of excavation
- How is the structure of IF7
- How is IAS more efficient than IPS
- What do you think of compulsory vaccinations
- Where does the word chicken come from?
- What should everyone know about YouTube
- Every moment is eternal
- Is it possible to live without trust?
- Is Ankara safe
- What is goodwill in accounting
- How can I become a neurobiologist
- Are the Chandigarh University law degrees worth it
- Serotonin Makes You Smarter
- How do ENTPs deal with small talk
- How is the extra low-frequency psychotronics encountered?
- Which online Bible offers the best UX
- Is Oracle no longer relevant
- An imprecise science is essentially an experiment
- Which zodiac is better than Pisces