

grep -C 5 search_pattern filename Show the lines that do not match The command below will show 5 lines before the matching one, the matching line and 5 lines after the matching line. My favorite is the option -C because it shows lines that are before and after the matching ones. The command below will show 5 lines before the matching ones along with the matching line(s). Similarly, you can use the -B option to show lines before the matching ones. The command below will show the matching lines along with the 5 lines after the match.

You can use the -A to show lines after the matching ones. But when you are troubleshooting something, it helps to see a couple of lines before and/or after the matching lines. Show lines before and after the matching linesīy default, you only see the matching lines. This way, grep will return lines that match both Holmes and holmes. You can ignore case matching with the -i option: grep -i search_pattern filename Case insensitive searchīy default, the search with grep is case-sensitive. Let's see a few common use cases of the grep command. You probably already know that to search for a particular text or pattern in a file, you have to use grep like this: grep search_pattern filename
#Grep cheat sheet pdf
I have included a PDF cheatsheet which you can download, print and keep on your desk. It's good if you are already familiar with the grep command but you keep forgetting which option does what. I'll just the common options and their explanation here. Linux Handbook already has a detailed article on grep, so I won't go in depth here. This article lists the most common grep commands with quick examples. However, you'll end up using a handful of grep commands most of the time. You probably will never need or use all of them. The grep command has a huge number of options and use cases. It's especially helpful when you are troubleshooting or debugging. Select the resultant file that works best or join them into a single cheat sheet.Grep is a powerful UNIX command that lets you search inside the file contents on a variety of parameters. Using the commands described in this post, you can assemble a list of commands and quickly prepare a cheat sheet that contains a brief description of each of them.
#Grep cheat sheet manual
, [, alias, bg, bind, break, builtin, caller, cd, command,Īpropos - search the manual page names and descriptionsĬat - concatenate files and print on the standard outputĬomm - compare two sorted files line by line The results below show the top 10 lines from each of the files. Man $cmd | head -4 | tail -1 2>/dev/null > help3 Whatis $cmd 2>/dev/null | grep ^$cmd 2>/dev/null > help2 # run through command list and collect descriptions

Note that it quiets any error message (e.g., when it can’t find a description for a particular command) and otherwise adds a line to one of the three files. This script calls the resultant files help1, help2, and help3.
#Grep cheat sheet series
To use each of the methods described, you can run a script like the one shown below that runs through the series of commands you provide (listed in a file) and creates a cheat sheet using each of the commands described. Date - print or set the system date and time Using a script
