0% found this document useful (0 votes)
1 views

Grep Commands

The document provides a guide on using grep commands, detailing various pattern matching techniques such as anchor characters, wildcards, escaped characters, character ranges, and repetition modifiers. It also lists options for modifying grep behavior, including case-insensitivity and line numbering. Several examples illustrate how to use these commands effectively in different scenarios.

Uploaded by

abhik.das84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Grep Commands

The document provides a guide on using grep commands, detailing various pattern matching techniques such as anchor characters, wildcards, escaped characters, character ranges, and repetition modifiers. It also lists options for modifying grep behavior, including case-insensitivity and line numbering. Several examples illustrate how to use these commands effectively in different scenarios.

Uploaded by

abhik.das84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Grep Commands

grep [options] [pattern] [file]

#1) Anchor Characters: ‘^’ and ‘$’ at the beginning and end of the pattern are used to
anchor the pattern to the start of the line, and to the end of the line respectively.

Example: “^Name” matches all lines that start with the string “Name”. The strings “\<”
and “\>” are used to anchor the pattern to the start and end of a word respectively.

#2) Wildcard Character: ‘.’ Is used to match any character.

Example:“^.$” will match all lines with any single character.

#3) Escaped Characters: Any of the special characters can be matched as a regular
character by escaping them with a ‘\’.

Example: “\$\*” will match the lines that contain the string “$*”

#4) Character Range: A set of characters enclosed in a ‘[‘ and ‘]’ pair specify a range of
characters to be matched.

Example: “[aeiou]” will match all lines that contain a vowel. A hyphen can be used while
specifying a range to shorten a set of consecutive characters. E.g. “[0-9]” will match all
lines that contain a digit. A carat can be used at the beginning of the range to specify a
negative range. E.g. “[^xyz]” will match all lines that do not contain x, y or z.

#5) Repetition Modifier: A ‘*’ after a character or group of characters is used to allow
matching zero or more instances of the preceding pattern.
The grep command supports a number of options for additional controls on the
matching:
 -i: performs a case-insensitive search.
 -n: displays the lines containing the pattern along with the line numbers.
 -v: displays the lines not containing the specified pattern.
 -c: displays the count of the matching patterns.

Examples:
 Match all lines that start with ‘hello’. E.g: “hello there”
$ grep “^hello” file1

 Match all lines that end with ‘done’. E.g: “well done”
$ grep “done$” file1

 Match all lines that contain any of the letters ‘a’, ‘b’, ‘c’, ‘d’ or ‘e’.
$ grep “[a-e]” file1

 Match all lines that do not contain a vowel


$ grep “[^aeiou]” file1

 Match all lines that start with a digit following zero or more spaces. E.g: “ 1.”
or “2.”
$ grep “ *[0-9]” file1

 Match all lines that contain the word hello in upper-case or lower-case
$ grep -i “hello”

You might also like