grep cheat sheet

Grep is a command-line tool that allows you to search for specific patterns or words in a file or group of files. It is commonly used by programmers, system administrators, and analysts to search for specific patterns in logs, configuration files, and other types of data.

To use grep, you specify the pattern you want to search for and the file or files you want to search in. Grep will then output all lines that contain the pattern.

Here is a basic example of using grep to search for the word “error” in a file called file.txt:

grep error file.txt

This will show all lines in file.txt that contain the word “error”.

Grep has many options that allow you to customize the search, such as ignoring case, showing line numbers, and searching multiple files. You can also use regular expressions, which are a powerful way to specify complex patterns to search for.

Here is my grep cheat sheet to help you get started with using grep.

OptionDescriptionExample
-vInvert the search, showing lines that do not match the patterngrep -v error file.txt
-eSpecify multiple patterns to search forgrep -e error -e warning file.txt
-fSpecify a file containing patterns to search for or excludegrep -f patterns.txt file.txt
-cCount the number of lines that match the patterngrep -c error file.txt
-iIgnore case when searchinggrep -i error file.txt
-nPrefix each line of output with the line numbergrep -n error file.txt
-lOnly show the names of files that contain a matchgrep -l error *.log
-rRecursively search subdirectoriesgrep -r error /var/log
-EInterpret the pattern as an extended regular expressiongrep -E '[0-9]*error' file.txt
-wOnly match whole wordsgrep -w error file.txt
-xOnly show lines that match the pattern exactlygrep -x error file.txt

Here is an explanation of each option:

  • -v: Inverts the search, showing lines that do not match the pattern. For example, grep -v error file.txt will show all lines in file.txt that do not contain the word “error”.
  • -e: Allows you to specify multiple patterns to search for. For example, grep -e error -e warning file.txt will show all lines in file.txt that contain either the word “error” or the word “warning”.
  • -f: Allows you to specify a file containing patterns to search for or exclude. For example, grep -f patterns.txt file.txt will show all lines in file.txt that contain any of the patterns in patterns.txt.
  • -c: Counts the number of lines that match the pattern. For example, grep -c error file.txt will show the number of lines in file.txt that contain the word “error”.
  • -i: Ignores case when searching. For example, grep -i error file.txt will show all lines in file.txt that contain the word “error”, regardless of whether it is capitalized or not.
  • -n: Prefixes each line of output with the line number. For example, grep -n error file.txt will show all lines in file.txt that contain the word “error”, along with the line number.
  • -l: Only shows the names of files that contain a match. For example, grep -l error *.log will show the names of all .log files that contain the word “error”.
  • -r: Recursively searches subdirectories. For example, grep -r error /var/log will search for the word “error” in all files under the /var/log directory, including subdirectories.
  • -E: Interprets the pattern as an extended regular expression. Regular expressions, or regex, are a powerful way to specify complex patterns to search for. Extended regular expressions add additional features, such as the ability to match multiple characters with a single expression. For example, grep -E '[0-9]*error' file.txt will show all lines in file.txt that contain a number followed by the word “error”.
  • -w: Only matches whole words. For example, grep -w error file.txt will show all lines in file.txt that contain the word “error” as a whole word, but will not match lines that contain the word “error” as a part of another word.
  • -x: Only shows lines that match the pattern exactly. For example, grep -x error file.txt will show all lines in file.txt that contain the word “error” and nothing else.

I hope this grep cheatsheet is helpful!

Related posts

Mastering the Linux ifconfig Command

Docker Exec Command With Practical Examples

Debugging with Git Bisect

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More