Linux-Ubuntu/C3/Introduction-to-grep-command/English
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:
|
| Slide 3
System Requirements
|
To record this tutorial, I am using
|
| Slide 4Pre-requisites
|
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.
These files are provided in the Code Files link of this tutorial page. |
The following code files are required to practice this tutorial.
|
| First let us learn about regular expressions. | |
| Slide 6
Regular Expressions |
|
| Slide 7
grep command |
|
| 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
|
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
|
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:
|
With this we come to the end of this tutorial.
Let us summarise. |
| Slide 9
Assignment As an assignment,
|
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. |