There comes a time in every Linux users life where they need to find an expression within a file. For example, you have a long log file and want to see all the instances mentioning a specific error. Grep is one command that can be used to accomplish this task.
In this article, we'll outline what is grep and how to use it.
What is grep?
Grep is a command included in most Linux distributions by default. It is an open source piece of software which allows you to search within a file to find instances of a particular expression.
What does grep do?
The grep command searches a text file based on a series of options and search string and returns the lines of the text file which contain the matching search string. The output can also be manipulated or piped into the console depending on what you need to do with the data.
How to use grep
At its core, you can type the command grep, followed by any options associated with the command, then the pattern within the file you want to find, followed by the file name
grep [OPTION]... PATTERN [FILE]...
The file named file2 has the following content:
Performing the following command executes a case sensitive regular expression search of the file:
grep cat file2
Returns the following result:
There are dozens of options to choose from when executing a grep command. Here are some of the common options:
Counts the number of instances of the search string in the file.
grep -c string filename
Displays the search results with the match search string in color.
grep --color string filename
Case insensitive search. Returns results ignoring whether or not the letter is uppercase or lowercase.
grep -i cat file2
Obtain the pattern to search from in a file.
grep -f filename file
Prints the line number that the expression appeared on when outputting the command.
grep -n cat file2
Returns all lines that do not contain the search string.
grep -v string filename
For extended expression searches. You can also use the egrep command
grep -E cat file2
Pattern is a set of fixed strings. You can also use the fgrep command.
greo -F cat file2
Performs a recursive search of the current directory and all sub-folders
grep -r searchstring
When entering the expression to search the file for, you do not need to limit yourself to a whole or partial word. You can use various characters in order to perform more expensive or specific searches.
Wildcard Search *
An asterisk can be inserted anywhere in the expression in order to return all results with any character in the expression.
c*t will return all results that begin with c and end with t, with any amount of characters in the middle.
*t will return all results that end with t
c* will return all results that start with c
grep c*t file2
grep *t file2
grep c* file2
Full word search
Adding quotes around a world will ensure grep only returns full word matches rather than any words that contain the search string.
grep "string" filename
Search for multiple words
The egrep command can be used to search for multiple words in a single grep search.
egrep 'word1|world2' filename
The output from the grep command can also be piped into another command or sent to a file depending on what you need to do with the data.
Outputting to File:
grep cat file2 > /home/grepoutput.txt
Piping into another command:
This command will look for the expression "cat" in the file named file2 and output it to the word count command wc. The -l option will then list the number of lines in the grep output.
grep cat file2 | wc -l
As you can see, grep is a powerful tool with a variety of command options and search parameters in order to return exactly the information you need. It is one of the most useful commands for linux administrators and you will no doubt find it useful in your administration tasks.
Have a question about grep? Leave a comment below!