Linux-Ubuntu/C3/Introduction-to-grep-command/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of the Script: Introduction to grep command.

Author: EduPyramids.

Keywords: grep, pattern matching, case-insensitive, inverse match, multiword search, line numbers, count matches, edupyramids, video tutorial.


Visual Cue Narration
Slide 1

Title Slide

Welcome to this spoken tutorial on Introduction to grep command.
Slide 2 Learning Objectives


In this tutorial, we will learn to:
  • View the content of a file
  • List entries of a particular stream
  • Ignore cases while searching
  • Handle lines that do not match a pattern
  • Display line numbers with the entries
  • Store the result in another file
  • Count the number of matches
Slide 3

System Requirements


To record this tutorial, I am using


Ubuntu OS version 24 point zero 4

Slide 4Pre-requisites

https://EduPyramids.org


To follow this tutorial,

Learners should have Ubuntu version 24 point zero 4.

And should be familiar with basic Linux terminal commands.

For the prerequisite Linux tutorials please visit this website.

Slide 5

Code Files

The following code files are required to practice this tutorial.

  1. grepdemo.txt
  2. igc-commands.txt

These files are provided in the Code Files link of this tutorial page.

The following code files are required to practice this tutorial.


These files are provided in the Code Files link of this tutorial page.

First let us learn about regular expressions.
Slide 6

Regular Expressions

  • Regular expressions are patterns used to match text.
  • They help to search and filter information in files.
  • They can match words, numbers, or character sequences.
  • In Linux, commands like grep use regular expressions.
Slide 7

grep command

  • grep is a Linux command used to search text.
  • It finds lines that match a given pattern.
  • It works on files or standard input.
  • It supports regular expressions for searching.
Download code file.Cut and paste to Home directory.open grepdemo.txt code file. We will now demonstrate the grep command using the grepdemo.txt file.

Download this file and move it to your home directory.

show the entries and point to the separation bar (|) This file contains 13 entries.Each entry has a roll number, name, stream, marks, result, and stipend.

The fields are separated by a vertical bar, which acts as a delimiter.

Note: Please type the commands on the terminal don't paste as the double quotes are wrong. Let us see how grep works.

Let’s find students from the computer’s stream.

Type

grep "computers" grepdemo.txt press enterShow side by side grepdemo.txt file entries.

At the terminal type, grep space double quote computers double quote space grepdemo dot t x t and press Enter.

The output shows entries from the computer’s stream.

Let us compare it with the original file.

Highlight Zubin entry in the file Notice that the entry for Zubin is missing.

This happens because grep is case sensitive.

Point to the capital C Why is this entry missing?

grep searched for computers in lowercase.

Zubin’s stream is written as Computers with a capital C.

Type grep -i "computers" grepdemo.txt Highlight -ipress Enter Pattern matching is case sensitive by default.

To make it case insensitive, we need to use the hyphen i option with grep.

Type this command and press Enter.

Point to the output. Now, all four matching entries are displayed.

Observe that, grep lists only the lines that match a given pattern.

We can also do the reverse.

Type grep -iv "pass" grepdemo.txt Highlight -vpress Enter

Point to the output.

Type this command. The hyphen v option lists the lines that do not match a pattern.

Press Enter to see the output.

Output shows students who failed or have incomplete results.

Show side by side home folder.

Type grep -iv "pass" grepdemo.txt > notpass.txt press Enter.

Let us store the output in a file named not pass dot t x t.

Type this command and press Enter.The file is created successfully.

Type cat notpass.txt press Enter. To view the students who have not passed, type cat space not pass dot t x t and press Enter.

Observe the output.

Type grep -i "fail" grepdemo.txt


Highlight output

Type this command to list the failed students and press Enter.

The output shows all students who have failed.

type

grep -in "fail" grepdemo.txt


Press Enter.

Let us use the hyphen n option to show line numbers with matching entries.

Type this command and press Enter.

Observe the output.

Type clear press Enter. Let us clear the terminal screen.
So far, we used single-word patterns.

For multi-word patterns, we have to put the entire pattern in quotes.

type

grep -i "ankit saraf" grepdemo.txt press enter

Type this command and press Enter.

We can see that Ankit Saraf's record is displayed.

At the prompt type

grep -i "fail" grepdemo.txt notpass.txt

press Enter.

We can also find patterns in multiple files.

Type this command and press Enter.

Observe the output.

Point to the output. While searching for multiple files, grep shows the filename for each match.

In this example, grep demo dot t x t and not pass dot t x t are searched.

Show only the inside files contents of notpass.txt and grepdemo.txt These are the records from not pass dot t x t file and these are the records from grep demo dot t x t file.
Type

grep -c “Fail” grepdemo.txt press Enter.

To display only the number of matches, use the hyphen c option.

Type this command and press the Enter key. Output shows the number of matches.

Slide 8

Summary

In this tutorial, we have learnt to:

  • View the content of a file
  • List entries of a particular stream
  • Ignore cases while searching
  • Handle lines that do not match a pattern
  • Display line numbers with the entries
  • Store the result in another file
  • Count the number of matches
With this we come to the end of this tutorial.

Let us summarise.

Slide 9

Assignment

As an assignment,

  1. Use grep to find all students in the “Maths” stream in grepdemo.txt.
  2. List lines that do not contain civil and save to nonCivil.txt.
  3. Show line numbers of entries containing fail.
  4. Search for a multi word pattern, for example Mani Chowdhari, in grepdemo.txt.


As an assignment, please do the following.
Slide 10

Thank you


This Spoken Tutorial is brought to you by EduPyramids Educational Services Private Limited, SINE, IIT Bombay. Thank you.

Contributors and Content Editors

Ketkinaina, Madhurig