0% found this document useful (0 votes)
4 views17 pages

Grep

The document provides an overview of the grep command, a command-line tool used for searching specific patterns in files or streams. It details the basic syntax, various options available for grep, and practical examples demonstrating its usage, including case-insensitive searches, counting matches, and matching whole words. Additionally, it presents case problems with solutions to illustrate the application of grep in different scenarios.

Uploaded by

Pulkit singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views17 pages

Grep

The document provides an overview of the grep command, a command-line tool used for searching specific patterns in files or streams. It details the basic syntax, various options available for grep, and practical examples demonstrating its usage, including case-insensitive searches, counting matches, and matching whole words. Additionally, it presents case problems with solutions to illustrate the application of grep in different scenarios.

Uploaded by

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

Text processing in

bash : grep

Pulkit Singh
A270150424001
05/07/2025

INTRODUCTION
• A command-line tool to search for specific patterns in files or streams.
• The grep command lets the user find text in a given file or out-put quickly and
easily.
• By giving grep a string to search for, it will print out only lines that contain that
string and can print the corresponding line numbers for that text.
• The “simple” use of the command is well-known, but there are a variety of more
advanced uses that make grep a powerful search tool.
• grep is actually a combination of four different tools, each with its unique style of
finding text: basic regular expressions, extended regular expressions, fixed strings,
and Perl-style regular expression.

2
05/07/2025

Basic Syntax of grep


• Command structure:

• Example:

Searches for the word 'error' in testfile.txt.


• Search multiple files for lines that match a pattern:

3
05/07/2025

Options Available in grep Command


Some options for grep include:
• -i Ignores, case for matching.
• -v This prints out all the lines that do not matches the pattern.
• -c This prints only a count of the lines that match a pattern.
• -l Lists filenames containing the match.
• -L Lists filenames that do not contain the match.
• -n Display the matched lines and their line numbers.
• -e Specifies expression with this option. Can use multiple times.
• -E Treats pattern as an extended regular expression (ERE).
• -w match whole word.
• -m limits the number of matches.
• -o Print only the matched parts of a matching line, with each such part on a separate output
line.

4
05/07/2025

Practical Example of grep


Command in Linux
• Case insensitive search
o The -i option enables to search for a string case insensitively in the given file. It matches
the words like “UNIX”, “Unix”, “unix”.

Input :
Output :

5
05/07/2025

Practical Example of grep


Command in Linux
• Displaying the Count of Number of Matches Using grep
o The option –c can find the number of lines that matches the given string/pattern

• Displaying only the matched pattern Using grep


o -o option can make the grep to display only the matched string.

6
05/07/2025

Practical Example of grep


Command in Linux
• Display the File Names that Matches the Pattern Using grep
o -l option displays the files that contains the given string/pattern.

Or

7
05/07/2025

Practical Example of grep


Command in Linux
• Show Line Number While Displaying the Output Using grep -n
o To show the line number of file with the line matched.

Checking for the Whole Words in a File Using grep


The -w option to grep makes it match only the whole words.

8
05/07/2025

Practical Example of grep


Command in Linux
• Inverting the Pattern Match Using grep
o -v option displays the lines that are not matched with the specified search string pattern.

9
05/07/2025

Practical Example of grep


Command in Linux
• Matching the Lines that Start with a String Using grep
o The ^ regular expression pattern specifies the start of a line. This can be used in grep to
match the lines which start with the given string or pattern.

10
05/07/2025

Practical Example of grep


Command in Linux
• Matching the Lines that End with a String Using grep
o The $ regular expression pattern specifies the end of a line. This can be used in grep to
match the lines which end with the given string or pattern.

11
05/07/2025

Practical Example of grep


Command in Linux
• Specifies expression with -e option
o Can use multiple times :

o Output

12
05/07/2025

Case problem
• Q1. Display lines containing 'to' from the 'testfile3.txt' input file.
o Solution:

o For whole word match lines:

13
05/07/2025

Case problem
• Q2. Display only the first two matching lines containing 'he' form 'testfile2.txt'
input file.
o Solution

• Q3. Display the first 5 matching lines that do not conatin 'on' from 'testfile.txt'
input file.
o Solution

14
05/07/2025

Case problem
• Q4. Display lines from 'testfile.txt' that contain 'is' along with line number prefix.
o Solution

• Q5. For the input file sample.txt, display lines containing amigo prefixed by the
input filename as well as the line number.

15
05/07/2025

Case problem
• Q6. Count the total number of lines containing banana in the input files
sample.txt and code.txt.
• Solution

• Q7. For the input file `testfile2.txt`, match all lines that start with 'cat` or end with
`sun`.
o Solution

16
05/07/2025

Case problem
• Q8. For the input file `testfile3.txt`, match all lines containing `on` but not as a
whole word.
o Solution

• Q9. Count the total number of times the whole words `removed` or `rested` or
`received` or `replied` or `refused` or `retired` are present in the `patterns.txt`
file.
o Solution

17

You might also like