0% found this document useful (0 votes)
47 views4 pages

Assignment 7

This document provides information about regular expressions (regex) and how to use them with the grep command in Linux. It defines what regex are, which are patterns used to match text. It explains the three main types of regex syntax supported by grep: basic, extended, and perl. It provides examples of common regex patterns and operators like brackets, caret, dollar sign, dot, star, and parentheses. It also covers anchors, back references, repetition operators, group operators, and commonly used grep options.

Uploaded by

yash
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)
47 views4 pages

Assignment 7

This document provides information about regular expressions (regex) and how to use them with the grep command in Linux. It defines what regex are, which are patterns used to match text. It explains the three main types of regex syntax supported by grep: basic, extended, and perl. It provides examples of common regex patterns and operators like brackets, caret, dollar sign, dot, star, and parentheses. It also covers anchors, back references, repetition operators, group operators, and commonly used grep options.

Uploaded by

yash
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/ 4

ASSIGNMENT 7

Expressions And Search Patterns

Linux comes with GNU grep, which supports extended regular expressions. GNU grep is the
default on all Linux systems. The grep command is used to locate information stored
anywhere on your server or workstation. Let us see fundamental of regex and how to use
regular expressions in the Linux and Unix like systems.

Regular Expressions in grep


Regular Expressions is nothing but a pattern to match for each input line. A
pattern is a sequence of characters. Following all are examples of pattern:

^w1
w1|w2
[^ ]
foo
bar
[0-9]

Three types of regex


The grep understands three different types of regular expression syntax as follows:

1. basic (BRE)
2. extended (ERE)
3. perl (PCRE)

grep Regular Expressions Examples


Search for a word named ‘vivek’ in the /etc/passwd file:
$ grep 'vivek' /etc/passwd
Regular Expression provides an ability to match a “string of text” in a very flexible and
concise manner. A “string of text” can be further defined as a single character, word,
sentence or particular pattern of characters.
Like the shell’s wild–cards which match similar filenames with a single expression, grep
uses an expression of a different sort to match a group of similar patterns.

 [ ]: Matches any one of a set characters


 [ ] with hyphen: Matches any one of a range characters
 ^: The pattern following it must occur at the beginning of each line
 ^ with [ ] : The pattern must not contain any character in the set specified
 $: The pattern preceding it must occur at the end of each line
 . (dot): Matches any one character
 \ (backslash): Ignores the special meaning of the character following it
 *: zero or more occurrences of the previous character
 (dot).*: Nothing or any numbers of characters.

Anchors
Anchors tell where the next character in the pattern must
be located in the text data.

BACK REFERENCES: \N

 used to retrieve saved text in one of nine buffers


 can refer to the text in a saved buffer by using a
back reference:
ex.: \1 \2 \3 ...\9
 more details on this later

Sequence Operator
In a sequence operator, if a series of atoms are shown in
a regular expression, there is no operator between them.
Repetition Operator: \{...\}

The repetition operator specifies that the atom or


expression immediately before the repetition may be
repeated.

Group Operator

In the group operator, when a group of characters is


enclosed in parentheses, the next operator applies to the
whole group, not only the previous characters.

GREP DETAIL AND EXAMPLES

 grep is family of commands


 grep
common version
 egrep
understands extended REs
(| + ? ( ) don’t need backslash)
 fgrep
understands only fixed strings, i.e. is faster
 rgrep
will traverse sub-directories recursively

23

COMMONLY USED “GREP” OPTIONS:


-c Print only a count of matched lines.
-i Ignore uppercase and lowercase distinctions.
-l List all files that contain the specified pattern.
-n Print matched lines and line numbers.

-s Work silently; display nothing except error messages.

Useful for checking the exit status.


-v Print lines that do not match the pattern.

You might also like