/regex/ Match lines matching the regular expression regex. Addr1,+N Will match addr1 and the N lines following addr1. Cregexc Match lines matching the regular expression regex. The c may be any character. Addr1,N Will match addr1 and the lines following addr1 until the next line whose input line number is a multiple of N. A regular expression, regex or regexp is a sequence of characters that define a search pattern. Usually such patterns are used by string searching algorithms for 'find' or 'find and replace' operations on strings, or for input validation.

Grep is a handy command-line utility that enables you to search text or files.

Grep regular expression cheat sheet

More formally: Grep (Global Regular Expression Print) is a utility for searching plain-text datasets for lines that match a given regular expression.

Grep was developed for Unix, but is now available for all unix-like systems (e.g. Linux).

Basic Command Syntax

Ignore Case

Use -i:

Recursive Search

Use -r:

To suppress the filename. use the -h option. This example shows suppressed filenames and sends the result output to a file:

Search for Whole Words

Grep will return lines that contain the target string - it may be a fragment of another string. To return only lines containing the target string as a distinct word, use the -w option:

Regular Expression Examples Linux

Search for many words requires egrep (extended grep), or escaping the pipe character:

Regex Cheat Sheet

References