In this tutorial, you will explore the grep command’s options, and then you’ll dive into using regular expressions to do more advanced searching. This seemingly trivial program is extremely powerful its ability to sort input based on complex rules makes it a popular link in many command chains. This means that you can use grep to check whether the input it receives matches a specified pattern. The name grep stands for “global regular expression print”. The -v flag ( grep -v) negates the search, so it will respond with everything that doesn't match the pattern.The grep command is one of the most useful commands in a Linux terminal environment.
The -i flag ( grep -i) makes the search case-insensitive.The red highlight makes it possible to see exactly which part of the string is matched. Adding the -color flag ( grep -color) will highlight the matched portion of the line in red.There are also some helpful flags you can use when executing grep: It is best to get into the habit of enclosing pattern argument in single-quotes when invoking grep to ensure the pattern is received as intended. Note that certain punctuation characters such as * and $ have special meaning to the shell and may get transformed before passing these arguments along to the program. match all words that are exactly 7 letters long.match all words that start with k and end with k.Here are some suggested exercises to use as practice in forming regular expressions: try grep joy /usr/share/dict/words or grep 'b.b' /usr/share/dict/words and see which words matched. This file is a good one to grep for practice, e.g. We can instead search grep "binky(.*)" program.cĪ dictionary word list is available on myth in the file /usr/share/dict/words. Grep is smart enough to backtrack after this failed match, and tries to match b* to the next longest string, b so that the final b in the search string can match the final b in the text.īuilding on the binky example from earlier, consider the case where binky() actually takes a number of arguments, so matching on "binky()" will not suffice. For example, when using ab*b to match abb, if one matches the b* to bb, then there is no text left to match the final b to. However, naive greedy matching strategy will sometimes miss matches. So therefore, ab* tries to match as many bs as possible instead of not matching any bs at all. This means that it tries to match as many characters as possible. Note that the * symbol does what we call greedy matching. * matches zero or more repeats of char to left of *Į.g., 'ab*'' matches 'abbbbb' and also 'a' Here are some of the core metacharacters that you will often use. grep especially shines is in matching complex patterns expressed as regular expressions (commonly shortened to regex). There is more to grep than just normal text matching. The general syntax for grep is grep įor example, if we were looking for all the lines where the binky function is called, we could use grep to search the source file as follows: grep "binky()" program.c Grep works like "find in file" (Ctrl-F or Command-F) search in Microsoft Word, or a search box on your computer's files and folders. Written by Chris Gregg, Prasanna Vasudevan and Rahul Agarwal, with modifications by Nick Troccoli