>grep_
grep
is a command that allows you to search occurrences of one or more
keywords in one or more files. Through some flags you can decide on the search criteria.
The command grep is case sensitive (robot is different from Robot), but we will see how
to ignore that.Here we have two files named
example1.txt
and
example2.txt
that contain 5 elements, to test this command:
example1.txt
example2.txt
The syntax command is:
grep [flag] [keyword] [file]You can put different flags together to refine the search.
grep Robot example1.txt example2.txt (if you write robot you won't have the correspondence.) example1.txt: Robot example2.txt: RobotYour output will be this because grep found the keyword Robot in both files
Flags
Here a list that contains the main flags of these command:- -i: this flag ignores case sensitivity. Here we can write Robot or
robot that grep will find the correspondence.
grep -i robot example1.txt example2.txt example1.txt: Robot example2.txt: Robot
- -v: this flag ignores the keyword from the search.
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
- -c: this flag indicates the number of times that the keyword appears in the file.
grep -i -c robot example1.txt 1
(If in the file you would have had two times the keyword Robot, the output would have been 2) - -h: Remove from the output the file name where it found the keyword
grep -i -h apple example1.txt example2.txt Apple
How you can see the output doesn't show the file nameexample2.txt