Its name comes from another similar command in ed tool, i.e., g/re/p which stands for globally search for a regular expression and print matching lines. grep basically searches for a given pattern or regular expression from standard input or file and prints the lines that match the given criteria. It’s often used to filter out unnecessary details while printing just the required information from big log files. The power of regular expression combines with supported options in grep makes this possible. Here we will be covering some of the commonly used grep command in different scenarios by sysadmin or developer. So let’s get started…👨‍💻

grep Command Syntax

grep command expects a pattern and optional arguments along with a file list if used without piping. A simple example is:

Searching Multiple Files

grep enables you to search for the given pattern not just in one but multiple files. Here’s how you can look for a pattern in multiple files by using * wildcard. Output: You can observe from the above output that the filename is printed first before printing the matching line to indicate where grep found the given pattern.

grep offers to search a pattern without looking at the case of the pattern. Use -i flag to tell grep to ignore case. Output:

It’s not always that we want a partial match but instead expect grep to match a complete word only. You can do that with -w flag. Output:

Check Match Count

Sometimes instead of the actual matched line, we need just the count of successful matches that grep made. We can get this count using -c option. Output:

Search Sub-directories

It’s often needed to search files not just in the current working directory but also in subdirectories. grep allows you to easily do that with -r flag. Output: As you can observe, grep traverses through each subdirectory inside a current directory and lists the files and lines where a match is found.

If you want to find something which doesn’t match a given pattern, grep allows doing just that with -v flag. Output: You can compare the output of grep command on the same pattern and file with and without -v flag. With -v, whichever lines don’t match the pattern gets printed.

grep allows you to print line numbers along with printed lines which makes it easy to know where the line is in the file. Use -n option as shown to get line numbers in output. Output:

Limit grep Output

For big files likes logs etc. grep output can be long and you may just need a fixed number of lines in the output instead of matching everything. We can use –m[num] to limit the printed lines by num. Here’s how to use it: Notice how the use of -m flag affects the output of grep for the same set of conditions in the example below:

Display Additional Lines

Often we need not just the lines which have a matching pattern but some lines above or below it for better context. It is possible to print a line above or below (or both) a line having a pattern using grep by using -A, -B or -C flags with num value. Here num denotes the number of additional lines to be printed which is just above or below the matched line. This is applicable to all matches that grep finds in the specified file or file list. OR OR Below output shows a normal grep output as well as output with flag -A, -B and -C one by one. Notice how grep interprets the flags and their values and the changes in respective output. With -A1 flag, grep prints 1 line which follows just after the matching line. Similarly, with -B1 flag, it prints 1 line just before the matching line. With -C1 flag, it prints 1 line which is before and after the matching line.

List File Names

To print just the name of the files where a pattern is found instead of actually matched lines, use -l flag. Here’s an example run:

Sometimes we need to print lines that match exactly with a given pattern, not some part of it. grep allows -x flag to do just that. In the below example, file.txt contains a line with just one word “support” and as such gets matched by grep with -x flag while ignoring lines that may contain the words “support” along with other text.

Match Starting String

Using regular expressions, we can find a string at the start of a line. Here’s how to do it. Example: Observe how using ^ character changes the output. ^ indicates the start of the string and grep matched ^It as any line starting with the word It. Enclosing in quotes can help when the pattern contains spaces etc.

Match Ending String

Another common useful regular expression is to match the end of the line pattern. Example: We tried to match a . character at the end of the line. Since dot (.) is a special meaning character, we need to escape it with \ character. Again notice how output varies when we just match . character and when we use $ to instruct grep to match only such lines which end with . (not the ones that may contain it anywhere in between).

Use Pattern File

There may be situations where you have some complex list of patterns that you use often. Instead of writing it down every time, you can specify a list of patterns in a file and use with -f flag. The file should contain one pattern per line. In our example, we’ve created pattern file names pattern.txt with the below contents: To use it, use -f flag.

Specify Multiple Patterns

grep allows specifying multiple patterns using -e flag. Example:

Specify Extended RegEx

grep also supports Extended Regular Expressions or ERE using -E flag. This is similar to egrep command in Linux. Using ERE has an advantage when you want to treat meta-characters as is and don’t want to substitute them as strings like grep. This gives you more flexibility in terms of escaping them as we’re required to do in the case of grep. That being said, using -E with grep is equivalent to egrep command. Here’s one use of ERE where we want to print lines that are not commented or blank. This is especially useful for finding something in big configuration files. I’ve additionally used -v flag to NOT print lines matching the pattern ‘^(#|$)’.

Conclusion

The above examples are just the tip of the iceberg. grep supports a range of options and can be a very useful tool in the hand of a person who knows how to effectively use it. We can not only use the examples given above but combine them in different ways to get what we need. Refer to its man page to read more about it. Next, learn SFTP command examples.

16 grep Command Examples to Help You in Real World - 9316 grep Command Examples to Help You in Real World - 1216 grep Command Examples to Help You in Real World - 8116 grep Command Examples to Help You in Real World - 2316 grep Command Examples to Help You in Real World - 3416 grep Command Examples to Help You in Real World - 8216 grep Command Examples to Help You in Real World - 2716 grep Command Examples to Help You in Real World - 3316 grep Command Examples to Help You in Real World - 6016 grep Command Examples to Help You in Real World - 8416 grep Command Examples to Help You in Real World - 3316 grep Command Examples to Help You in Real World - 5316 grep Command Examples to Help You in Real World - 1016 grep Command Examples to Help You in Real World - 2316 grep Command Examples to Help You in Real World - 216 grep Command Examples to Help You in Real World - 1216 grep Command Examples to Help You in Real World - 2416 grep Command Examples to Help You in Real World - 2016 grep Command Examples to Help You in Real World - 3816 grep Command Examples to Help You in Real World - 1416 grep Command Examples to Help You in Real World - 5216 grep Command Examples to Help You in Real World - 5316 grep Command Examples to Help You in Real World - 39