2018-11-11 12:37:16 +00:00
|
|
|
---
|
|
|
|
layout: page
|
|
|
|
author: Domenico Votta
|
2018-11-11 16:47:25 +00:00
|
|
|
category-page: advanced
|
|
|
|
category-title: Advanced commands
|
2018-11-14 08:56:17 +00:00
|
|
|
tags: search for occurencies
|
2018-11-11 12:37:16 +00:00
|
|
|
title: grep
|
2018-11-18 20:38:56 +00:00
|
|
|
previous-page: pages/cmd/advanced/emacs.html
|
|
|
|
next-page: pages/cmd/advanced/head-tail.html
|
2018-11-11 12:37:16 +00:00
|
|
|
---
|
2018-11-14 21:15:38 +00:00
|
|
|
The <code>grep</code> is a command that permits to search occurences of a
|
2018-11-14 08:56:17 +00:00
|
|
|
keyword or more in a file or more. Through some flags you can decide the search criteria.
|
|
|
|
The command grep is case sensitive (robot is different from Robot), but we will see how
|
|
|
|
to ignore it.<br>
|
|
|
|
|
|
|
|
Here we have two files named <code>example1.txt</code> and
|
|
|
|
<code>example2.txt</code> that contain 5 elements, to test this command:
|
2018-11-11 12:37:16 +00:00
|
|
|
|
|
|
|
<code>example1.txt</code>
|
2018-11-18 17:39:51 +00:00
|
|
|
{% highlight bash %}
|
2018-11-11 12:37:16 +00:00
|
|
|
Car
|
|
|
|
Computer
|
|
|
|
Robot
|
|
|
|
Smartphone
|
|
|
|
Videogame
|
|
|
|
{% endhighlight %}
|
|
|
|
|
|
|
|
<code>example2.txt</code>
|
2018-11-18 17:39:51 +00:00
|
|
|
{% highlight bash %}
|
2018-11-11 12:37:16 +00:00
|
|
|
Apple
|
|
|
|
Computer
|
|
|
|
Robot
|
|
|
|
Microsoft
|
|
|
|
Huawei
|
|
|
|
{% endhighlight %}
|
|
|
|
|
|
|
|
The syntax command is:
|
|
|
|
<pre>grep [flag] [keyword] [file]</pre>
|
|
|
|
You can put different flags together to refine the search.
|
|
|
|
|
2018-11-14 08:56:17 +00:00
|
|
|
<pre>
|
2018-11-11 12:37:16 +00:00
|
|
|
grep Robot example1.txt example2.txt (if you write robot you won't have the correspondence.)
|
2018-11-14 08:56:17 +00:00
|
|
|
example1.txt: Robot
|
|
|
|
example2.txt: Robot
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
Your output will be this because grep found the keyword Robot in both files
|
2018-11-11 12:37:16 +00:00
|
|
|
|
|
|
|
<h2>Flags</h2>
|
2018-11-14 08:56:17 +00:00
|
|
|
Here a list that contains the main flags of these command:
|
2018-11-11 12:37:16 +00:00
|
|
|
|
|
|
|
<ul>
|
2018-11-14 08:56:17 +00:00
|
|
|
<li> <b>-i</b>: this flag ignore the case sensitive. Here we can write Robot or
|
|
|
|
robot that grep will find the correspondence.
|
2018-11-11 16:47:25 +00:00
|
|
|
<pre>
|
2018-11-11 12:37:16 +00:00
|
|
|
grep -i robot example1.txt example2.txt
|
2018-11-14 08:56:17 +00:00
|
|
|
example1.txt: Robot
|
|
|
|
example2.txt: Robot
|
2018-11-11 16:47:25 +00:00
|
|
|
</pre>
|
2018-11-14 08:56:17 +00:00
|
|
|
</li>
|
2018-11-11 12:37:16 +00:00
|
|
|
|
2018-11-14 08:56:17 +00:00
|
|
|
<li> <b>-v</b>: this flag ignore the keyword from the search.
|
2018-11-11 16:47:25 +00:00
|
|
|
<pre>
|
2018-11-11 12:37:16 +00:00
|
|
|
grep -i -v robot example1.txt example2.txt
|
2018-11-14 08:56:17 +00:00
|
|
|
example1.txt:Car
|
|
|
|
example1.txt:Computer
|
|
|
|
example1.txt:Smartphone
|
|
|
|
example1.txt:Videogame
|
|
|
|
example2.txt:Apple
|
|
|
|
example2.txt:Computer
|
|
|
|
example2.txt:Microsoft
|
|
|
|
example2.txt:Huawei
|
2018-11-11 16:47:25 +00:00
|
|
|
</pre>
|
2018-11-14 08:56:17 +00:00
|
|
|
</li>
|
2018-11-11 12:37:16 +00:00
|
|
|
|
2018-11-14 08:56:17 +00:00
|
|
|
<li> <b>-c</b>: this flag indicates the number of times that the keyword appears in the file.
|
2018-11-11 16:47:25 +00:00
|
|
|
<pre>
|
2018-11-11 12:37:16 +00:00
|
|
|
grep -i -c robot example1.txt
|
2018-11-14 08:56:17 +00:00
|
|
|
1
|
2018-11-11 16:47:25 +00:00
|
|
|
</pre>
|
2018-11-14 08:56:17 +00:00
|
|
|
(If in the file you would have had two times the keyword Robot, the output would have been 2)
|
|
|
|
</li>
|
2018-11-11 12:37:16 +00:00
|
|
|
|
2018-11-14 08:56:17 +00:00
|
|
|
<li> <b>-h</b>: Remove from the output the file name where it found the keyword
|
2018-11-11 16:47:25 +00:00
|
|
|
<pre>
|
2018-11-11 12:37:16 +00:00
|
|
|
grep -i -h apple example1.txt example2.txt
|
2018-11-14 08:56:17 +00:00
|
|
|
Apple
|
2018-11-11 16:47:25 +00:00
|
|
|
</pre>
|
2018-11-14 08:56:17 +00:00
|
|
|
How you can see the output doesn't show tha file name <code>example2.txt</code>
|
|
|
|
</li>
|
2018-11-11 12:37:16 +00:00
|
|
|
</ul>
|