f6f42bfc01
git-svn-id: svn+ssh://atelier.inf.usi.ch/home/bevilj/group-1@105 a672b425-5310-4d7a-af5c-997e18724b81
80 lines
2.2 KiB
HTML
80 lines
2.2 KiB
HTML
---
|
|
layout: page
|
|
author: Domenico Votta
|
|
category-page: advanced
|
|
category-title: Advanced commands
|
|
tags: search for occurences
|
|
title: grep
|
|
---
|
|
<p>The <code>grep</code> is a command that permits to search occurences of a 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</p>
|
|
Here we have two files named <code>example1.txt</code> and <code>example2.txt</code> that contain 5 elements, to test this command:
|
|
|
|
<code>example1.txt</code>
|
|
{% highlight bash linenos %}
|
|
Car
|
|
Computer
|
|
Robot
|
|
Smartphone
|
|
Videogame
|
|
{% endhighlight %}
|
|
|
|
<code>example2.txt</code>
|
|
{% highlight bash linenos %}
|
|
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.
|
|
|
|
{% highlight ruby linenos %}
|
|
grep Robot example1.txt example2.txt (if you write robot you won't have the correspondence.)
|
|
example1.txt: Robot
|
|
example2.txt: Robot
|
|
{% endhighlight %}
|
|
<p>Your output will be this because grep found the keyword Robot in both files</p>
|
|
|
|
<h2>Flags</h2>
|
|
<p>Here a list that contains the main flags of these command:
|
|
|
|
<ul>
|
|
<li> <b>-i</b>: this flag ignore the case sensitive. Here we can write Robot or robot that grep will find the correspondence. </li>
|
|
<pre>
|
|
grep -i robot example1.txt example2.txt
|
|
example1.txt: Robot
|
|
example2.txt: Robot
|
|
</pre>
|
|
|
|
<li> <b>-v</b>: this flag ignore the keyword from the search. </li>
|
|
<pre>
|
|
grep -i -v robot example1.txt example2.txt
|
|
example1.txt:Car
|
|
example1.txt:Computer
|
|
example1.txt:Smartphone
|
|
example1.txt:Videogame
|
|
example2.txt:Apple
|
|
example2.txt:Computer
|
|
example2.txt:Microsoft
|
|
example2.txt:Huawei
|
|
</pre>
|
|
|
|
<li> <b>-c</b>: this flag indicates the number of times that the keyword appears in the file. </li>
|
|
<pre>
|
|
grep -i -c robot example1.txt
|
|
1
|
|
</pre>
|
|
<p> If in the file you would have had two times the keyword Robot, the output would have been 2</p>
|
|
|
|
<li> <b>-h</b>: Remove from the output the file name where it found the keyword</li>
|
|
<pre>
|
|
grep -i -h apple example1.txt example2.txt
|
|
Apple
|
|
</pre>
|
|
<p>How you can see the output doesn't show tha file name <code>example2.txt</code></p>
|
|
</ul>
|
|
|
|
</p>
|