grep
stands for Globally Search For Regular Expression and Print out. It is a command line tool used in UNIX and Linux systems to search a specified pattern in a file or group of files.
grep
comes with a lot of options which allow us to perform various search-related actions on files. In this article, we’ll look at how to use grep
with the options available as well as basic regular expressions to search files.
**grep**
Without passing any option, grep
can be used to search for a pattern in a file or group of files. The syntax is:
grep '<text-to-be-searched>' <file/files>
Note that single or double quotes are required around the text if it is more than one word.
You can also use the wildcard (*) to select all files in a directory.
The result of this is the occurences of the pattern (by the line it is found) in the file(s). If there is no match, no output will be printed to the terminal.
For example, say we have the following files (called grep.txt):
Hello, how are you
I am grep
Nice to meet you
The following grep
command will search for all occurences of the word ‘you’:
grep you grep.txt
The result for this is:
Hello, how are you
Nice to meet you
you
is expected to have a different color than the other text to easily identify what was searched for.
But grep
comes with more options which help us achieve more during a search operation. Let’s look at nine of them while applying them to the example above.
#linux