Difference between revisions of "Linux-Ubuntu/C3/Introduction-to-grep-command/English"
Ketkinaina (Talk | contribs) (Created page with "'''Title of the Script: Introduction to grep command. '''Author:''' EduPyramids.''' '''Keywords:''' '''grep, pattern matching, case-insensitive, inverse match, multiword sea...") |
|||
| Line 16: | Line 16: | ||
|| Welcome to this spoken tutorial on '''Introduction to grep command'''. | || Welcome to this spoken tutorial on '''Introduction to grep command'''. | ||
|- | |- | ||
| − | || '''Slide 2 Learning Objectives | + | || '''Slide 2 Learning Objectives''' |
| − | || In this tutorial, we will learn to:* View the content of a file | + | || In this tutorial, we will learn to: |
| + | * View the content of a file | ||
* List entries of a particular stream | * List entries of a particular stream | ||
* Ignore cases while searching | * Ignore cases while searching | ||
| Line 27: | Line 28: | ||
* Store the result in another file | * Store the result in another file | ||
* Count the number of matches | * Count the number of matches | ||
| − | |||
| − | |||
| − | |||
|- | |- | ||
|| '''Slide 3''' | || '''Slide 3''' | ||
| − | '''System | + | '''System Requirements''' |
| + | || To record this tutorial, I am using | ||
| − | |||
'''Ubuntu OS '''version 24 point zero 4 | '''Ubuntu OS '''version 24 point zero 4 | ||
| Line 45: | Line 43: | ||
'''https://EduPyramids.org''' | '''https://EduPyramids.org''' | ||
| − | |||
| Line 54: | Line 51: | ||
And should be familiar with basic Linux terminal commands. | And should be familiar with basic Linux terminal commands. | ||
| − | For the prerequisite | + | For the prerequisite Linux tutorials please visit this website. |
|- | |- | ||
|| '''Slide 5''' | || '''Slide 5''' | ||
| Line 60: | Line 57: | ||
'''Code Files''' | '''Code Files''' | ||
| − | The following code files are required to practice this tutorial.# '''grepdemo.txt''' | + | The following code files are required to practice this tutorial. |
| + | |||
| + | # '''grepdemo.txt''' | ||
# '''igc-commands.txt''' | # '''igc-commands.txt''' | ||
| − | |||
| − | |||
These files are provided in the Code Files link of this tutorial page. | These files are provided in the Code Files link of this tutorial page. | ||
| Line 71: | Line 68: | ||
These files are provided in the Code Files link of this tutorial page. | These files are provided in the Code Files link of this tutorial page. | ||
|- | |- | ||
| − | | | | + | || |
|| First let us learn about regular expressions. | || First let us learn about regular expressions. | ||
|- | |- | ||
| Line 77: | Line 74: | ||
'''Regular Expressions''' | '''Regular Expressions''' | ||
| − | || * Regular expressions are patterns used to match text. | + | || |
| + | * Regular expressions are patterns used to match text. | ||
* They help to search and filter information in files. | * They help to search and filter information in files. | ||
* They can match words, numbers, or character sequences. | * They can match words, numbers, or character sequences. | ||
* In Linux, commands like '''grep''' use regular expressions. | * In Linux, commands like '''grep''' use regular expressions. | ||
| − | |||
|- | |- | ||
| Line 87: | Line 84: | ||
'''grep command ''' | '''grep command ''' | ||
| − | || * '''grep''' is a '''Linux''' command used to search text. | + | || |
| + | * '''grep''' is a '''Linux''' command used to search text. | ||
* It finds lines that match a given pattern. | * It finds lines that match a given pattern. | ||
* It works on files or standard input. | * It works on files or standard input. | ||
* It supports regular expressions for searching. | * It supports regular expressions for searching. | ||
| − | |||
|- | |- | ||
| Line 112: | Line 109: | ||
'''grep "computers" grepdemo.txt '''press enterShow side by side '''grepdemo.txt '''file entries. | '''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. | + | || 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. | Let us compare it with the original file. | ||
|- | |- | ||
|| Highlight Zubin entry in the file | || Highlight Zubin entry in the file | ||
| − | || Notice that the entry for Zubin is missing. This happens because '''grep''' is case sensitive. | + | || Notice that the entry for Zubin is missing. |
| + | |||
| + | This happens because '''grep''' is case sensitive. | ||
|- | |- | ||
|| Point to the capital C | || Point to the capital C | ||
| Line 127: | Line 128: | ||
|- | |- | ||
|| Type '''grep -i "computers" grepdemo.txt '''Highlight '''-i'''press '''Enter''' | || Type '''grep -i "computers" grepdemo.txt '''Highlight '''-i'''press '''Enter''' | ||
| − | || Pattern matching is case sensitive by default.To make it case insensitive, we need to use the '''hyphen i '''option with '''grep'''. | + | || 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'''. | Type this command and press '''Enter'''. | ||
| Line 141: | Line 144: | ||
Point to the output. | 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. | + | || 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. | 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'''. | + | || 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'''. | || 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 this command and press''' Enter'''.The file is created successfully. | ||
|- | |- | ||
|| Type '''cat notpass.txt '''press '''Enter'''. | || 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. | + | || 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 '''grep -i "fail" grepdemo.txt ''' | ||
| Line 158: | Line 166: | ||
Highlight''' '''output | Highlight''' '''output | ||
| − | || Type this command to list the failed students and press '''Enter'''.The output shows all students who have failed. | + | || Type this command to list the failed students and press '''Enter'''. |
| + | |||
| + | The output shows all students who have failed. | ||
|- | |- | ||
|| type | || type | ||
| Line 165: | Line 175: | ||
| − | + | Press Enter. | |
|| Let us use the '''hyphen n '''option to show line numbers with matching entries. | || Let us use the '''hyphen n '''option to show line numbers with matching entries. | ||
| Line 175: | Line 185: | ||
|| Let us clear the terminal screen. | || 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. | + | || So far, we used single-word patterns. |
| + | |||
| + | For multi-word patterns, we have to put the entire pattern in quotes. | ||
|- | |- | ||
|| type | || type | ||
| Line 190: | Line 202: | ||
press''' Enter'''. | press''' Enter'''. | ||
| − | || We can also find patterns in multiple files.Type this command and press''' Enter'''. | + | || We can also find patterns in multiple files. |
| + | |||
| + | Type this command and press''' Enter'''. | ||
Observe the output. | Observe the output. | ||
| Line 231: | Line 245: | ||
'''Assignment''' | '''Assignment''' | ||
| − | As an assignment# Use grep to find all students in the “Maths” stream in grepdemo.txt. | + | As an assignment, |
| + | |||
| + | # Use grep to find all students in the “Maths” stream in grepdemo.txt. | ||
# List lines that do '''not''' contain civil and save to '''nonCivil.txt'''. | # List lines that do '''not''' contain civil and save to '''nonCivil.txt'''. | ||
# Show line numbers of entries containing fail. | # Show line numbers of entries containing fail. | ||
Revision as of 17:06, 25 January 2026
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. |