Grep CheatSheet

The Grep cheat sheet is a valuable resource for anyone looking to improve their command-line skills. With its clear and concise explanations of basic syntax, advanced query techniques, and common use cases, this cheat sheet is an indispensable tool for mastering the power of Grep.

Whether you're a seasoned developer or just starting out, the Grep cheat sheet is designed to help you quickly and easily find the information you need to perform complex searches, filter results, and extract patterns from your data. Plus, with its detailed examples and practical tips, you'll be able to apply what you learn right away in your own work.

So if you want to take your command-line skills to the next level, download the Grep cheat sheet today. It's the perfect way to become a Grep expert and boost your productivity on the command line!


Table of Content




# Getting started Grep


What is Grep Command ?

grep is a command-line utility for searching plain-text data sets for lines that match a regular expression. Its name comes from the ed command g/re/p (globally search for a regular expression and print matching lines), which has the same effect.

grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others such as OS-9.

GREP Command Usage Benefits

'grep' command is the prime weapon in the war zone of Linux. Once you master the GREP command, you should get the tons of benefits out of it.

  1. Saves time over finding the required configuration
  2. Solves the problem related to the troubleshooting more quickly
  3. Help for debugging the code more quickly
  4. Finding out the blank files and folders in Linux

How to use Grep Command

Most people imagine that system administrators and programmers fiddle with knobs and diodes. Their goal? To reach into the virtual reality of the Internet, gathering the binary forces of code into the applications and infrastructure we all use today. Most people would be disappointed to learn that sysadmins and code monkeys more often poke at streams of text in hopes of getting the right response.

If you are a sysadmin or programmer and find yourself obsessively dipping into streams of text on a POSIX system, then you have probably either encountered grep or come across a time you wished you could use this command.

Its quite easy to make use of the grep command in your Linux- or UNIX-based system. As you might already have an idea that for using this command, you need to open the terminal on your system.

Now to use the grep command, the first thing you need to do is to understand its syntax, which is as follows:


grep [OPTION]  PATTERNS  [FILE]
  • OPTIONS - Defines the way the grep will behave while searching for a text or string.
  • PATTERNS - The text pattern or the regular expressions that need to be searched.
  • FILE - It specifies one or more files in which the pattern will be searched.

Use Case

We assume, a Security analyst is troubleshooting the logs for a recent attack. In this use case, the analyst may have around GBs, which takes an ample amount of time to troubleshoot the issue. By that time, your website is down, that impact is different.

However, Using the grep command Security Analyst can easily find out the required set of logs with the Parameter he/she searching in the captured logs, that too in a brief period.

Usage

# Search standard output (i.e. a stream of text)
$ grep [options] search_string


# Search for an exact string in file:
$ grep [options] search_string path/to/file


# Print lines in bestforstudy.txt containing the string "bestforstudy"
$ grep 'bestforstudy' bestforstudy.txt

Option examples

Option Example Operation
-i grep -i ^DA demo.txt Forgets about case sensitivity
-w grep -w "of" demo.txt Search only for the full word
-A grep -A 3 'Exception' error.log Display 3 lines after matching string
-B grep -B 4 'Exception' error.log Display 4 lines before matching string
-C grep -C 5 'Exception' error.log Display 5 lines around matching string
-r grep -r 'quickref.me' /var/log/nginx/ Recursive search (within subdirs)
-v grep -v 'warning' /var/log/syslog Return all lines which don't match the pattern
-e grep -e '^al' filename Use regex (lines starting with 'al')
-E grep -E 'ja(s|cks)on' filename Extended regex (lines containing jason or jackson)
-c grep -c 'error' /var/log/syslog Count the number of matches
-l grep -l 'robot' /var/log/* Print the name of the file(s) of matches
-o grep -o search_string filename Only show the matching part of the string
-n grep -n "go" demo.txt Show the line numbers of the matches

Important Note

The abbreviation grep stands for Global Regular Expression Print. This is one of the most widely used commands in the Linux world. Approx., 90% of users, use the grep command to find the matching pattern/string in a file. However, we always miss the real power of the grep command. The regular expression capability makes it one of the prime commands of the Linux world. Which helps a lot to process the data or analyze the broad set of logs.

A tool like grep is indeed extremely useful, not to mention indispensable, for looking through a multitude of text files, scripts, and especially logs for specific patterns with considerable ease. You can look for one or several patterns in a single file or many files, or use them to filter out the lines from those files containing those patterns.

?

You can even make use of pipes for more complicated searches and filtering using grep. It is well worth the time to learn more about this excellent command, especially some of its advanced features.

# Regular Expressions in Grep


Refer

Wildcards

- -
. Any character.
? Optional and can only occur once.
* Optional and can occur more than once.
+ Required and can occur more than once.

Quantifiers

- -
{n} Previous item appears exactly n times.
{n,} Previous item appears n times or more.
{,m} Previous item appears n times maximum.
{n,m} Previous item appears between n and m times.

POSIX

- -
[:alpha:] Any lower and upper case letter.
[:digit:] Any number.
[:alnum:] Any lower and upper case letter or digit.
[:space:] Any whites­pace.

Character

- -
[A-Z­a-z] Any lower and upper case letter.
[0-9] Any number.
[0-9­A-Z­a-z] Any lower and upper case letter or digit.

Position

^ Beginning of line.
$ End of line.
^$ Empty line.
\< Start of word.
\> End of word.


Best Suggest