SlideShare a Scribd company logo
3
Most read
5
Most read
7
Most read
Introduction to Unix and OS
Module 3
grep and Regular Expression
Dr. Girisha G S
Dept. of CSE
SoE,DSU, Bengaluru
1
Basic Regular Expression
What is regular expression?
- Regular expression is a pattern which contains meta characters for a complex
search/match
Classification of regular expression
1. Basic Regular Expression (BRE)
2. Extended Regular Expression (ERE)
- grep command supports
-> BRE by default
-> ERE with the –E option
- Basic Regular Expression make it possible to match a group of similar patterns
The BRE character subset
Meta character /
character
Matches
* Zero or more occurrences of preceding expression
E.g. a* matches no ‘a’, a, aa, aaa, …….
. A single character
E.g. .* matches any number of characters
[ ] Matches single character with in the brackets
E.g. [pqr] matches a single character p, q, or r
[1-4] matches digit between 1 & 3
^ Match a pattern at the beginning of line
E.g. ^The match any line which begin with the word
The
$ Match a pattern at the end of line
E.g. ab$ matches any stream that end with b
The character class ( [ ] )
- matches one of the character in the brackets
E.g. To match Agarwal or agrawal, the RE is
[Aa]g[ar][ra]wal
The *
Matches zero or more occurrences of the preceding expression
E.g.1 g* matches single character g or any no of g’s
i.e. no ‘g’, g, gg, ggg, …….
E.g.2 To match the pattern Agarwal, agrawal, aggarwal, the RE is
[Aa]gg*[ar][ra]wal
The Dot(.)
- A dot(.) matches single character
- .* sigifies any no of character or none
E.g. Match all 4 character words beginning with 2
2…
Specifying pattern location (^ & $)
E.g. 1. Match lines starting with 2
^2
E.g. 2 match lines where salary between no 7000 to 7999
7…$
grep & RE
- You can also use regular expression with grep command
Example1: search files Agarwal and agrawal from emp.lst
$ grep ‘[aA]g[ar[ar]wal emp.lst
Example2: search for the lines Agarwal, agrawal, aggarwal from emp.lst
$ gepr ‘[aA]gg*[ar[ar]wal’ emp.lst
Example3: print all lines that begin with letter ‘a ‘ followed by one
character followed by letter sequence ‘ple’ from fruitlist.txt
$ grep ‘^a.ple’ fruitlist.txt
Example4: print all the lines ending with letter ‘e’ from sample.txt
$ grep ‘e$’ sample.txt
Extended Regular Expression
- Extended RE make it possible to match disimilar patterns with
a single expression
- This set uses additional characters
- grep command use them with –E option
The ERE set
+ matches one or more occurrence of the previous character
E.g a+ matches a,aa,aaa,……..
[ab]+ matches ab,abaab, ababab,…..
[a-az]+ matches all string of lower characters
? Matches zero or q occurrence of preceding expression
E.g. ab?c matches either ac or abc, here b is optiona
| Indicates alternation
E.g . (ab|cd) matches either ab or cd
( ) groups a series of REs together into a new RE
grep and ERE
- Use grep’s -E option to use ERE
Example1: search for the lines Agarwal, aggarwal from the file
emp.lst
$ gepr -E ‘[aA]gg?arwal’ emp.lst
Example2: search for the lines segupta and dasgupta from
the file emp.lst
$ grep –E ‘(sen|das)gupta’ emp.lst

More Related Content

PPTX
Asymptotic notations
PPT
17. Trees and Graphs
PPTX
Normal forms
PPTX
PPSX
Functional dependency
PPTX
Context free grammar
PPTX
form view
PPTX
Matrix representation of graph
Asymptotic notations
17. Trees and Graphs
Normal forms
Functional dependency
Context free grammar
form view
Matrix representation of graph

What's hot (20)

PPT
6. Integrity and Security in DBMS
PPTX
Regular expression to NFA (Nondeterministic Finite Automata)
PPTX
Network flows
PPTX
Introduction to data structure ppt
PPT
pointer, structure ,union and intro to file handling
PPT
Graphs - Discrete Math
PPTX
Functions in C
PPTX
Linked list
PPTX
Decomposition using Functional Dependency
PPTX
Generating code from dags
PPTX
Functions in c language
PPT
Sum of subsets problem by backtracking 
PPTX
Unit 2 linked list
PPSX
INTRODUCTION TO C PROGRAMMING
PDF
Learning c - An extensive guide to learn the C Language
PPTX
Data structure - Graph
PPTX
Input-Buffering
PPTX
Linked list
6. Integrity and Security in DBMS
Regular expression to NFA (Nondeterministic Finite Automata)
Network flows
Introduction to data structure ppt
pointer, structure ,union and intro to file handling
Graphs - Discrete Math
Functions in C
Linked list
Decomposition using Functional Dependency
Generating code from dags
Functions in c language
Sum of subsets problem by backtracking 
Unit 2 linked list
INTRODUCTION TO C PROGRAMMING
Learning c - An extensive guide to learn the C Language
Data structure - Graph
Input-Buffering
Linked list
Ad

Similar to Basic regular expression, extended regular expression (20)

PPT
Bioinformatica 06-10-2011-p2 introduction
DOCX
Regular expressionfunction
PPT
Regular Expressions grep and egrep
PDF
Module 3 - Regular Expressions, Dictionaries.pdf
PPTX
Chapter 3: Introduction to Regular Expression
PDF
Maxbox starter20
PPTX
Bioinformatics p2-p3-perl-regexes v2014
PDF
Python strings
PPTX
Regular expressions in Python
PPT
PHP - Introduction to String Handling
PPTX
Strings,patterns and regular expressions in perl
PPTX
Unit 1-strings,patterns and regular expressions
PPTX
Regular_Expressions.pptx
PDF
Python regular expressions
PPT
PHP Regular Expressions
PPTX
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
PPT
Regular expressions
PPT
Php Chapter 4 Training
PPTX
Regex lecture
PDF
Perl_Part4
Bioinformatica 06-10-2011-p2 introduction
Regular expressionfunction
Regular Expressions grep and egrep
Module 3 - Regular Expressions, Dictionaries.pdf
Chapter 3: Introduction to Regular Expression
Maxbox starter20
Bioinformatics p2-p3-perl-regexes v2014
Python strings
Regular expressions in Python
PHP - Introduction to String Handling
Strings,patterns and regular expressions in perl
Unit 1-strings,patterns and regular expressions
Regular_Expressions.pptx
Python regular expressions
PHP Regular Expressions
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Regular expressions
Php Chapter 4 Training
Regex lecture
Perl_Part4
Ad

More from Dr. Girish GS (14)

PPTX
Unix- the process
PPTX
unix- Sort, uniq,tr,grep
PPTX
unix- the process states, zombies, running jobs in background
PPTX
Unix - Filters
PPTX
Customizing the unix environment
PPTX
File systems and inodes
PPTX
Loop instruction, controlling the flow of progam
PPT
Logic instructions part 1
PPTX
Bcd arithmetic instructions
PPTX
Bcd and ascii arithmetic instructions
PPTX
Ascii arithmetic instructions
PPT
Rotate instructions
PPTX
Program control instructions
PPTX
Memory interface
Unix- the process
unix- Sort, uniq,tr,grep
unix- the process states, zombies, running jobs in background
Unix - Filters
Customizing the unix environment
File systems and inodes
Loop instruction, controlling the flow of progam
Logic instructions part 1
Bcd arithmetic instructions
Bcd and ascii arithmetic instructions
Ascii arithmetic instructions
Rotate instructions
Program control instructions
Memory interface

Recently uploaded (20)

PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Artificial Intelligence
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
web development for engineering and engineering
DOCX
573137875-Attendance-Management-System-original
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT
Project quality management in manufacturing
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Construction Project Organization Group 2.pptx
OOP with Java - Java Introduction (Basics)
Artificial Intelligence
Operating System & Kernel Study Guide-1 - converted.pdf
Foundation to blockchain - A guide to Blockchain Tech
web development for engineering and engineering
573137875-Attendance-Management-System-original
CYBER-CRIMES AND SECURITY A guide to understanding
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Safety Seminar civil to be ensured for safe working.
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Project quality management in manufacturing
Automation-in-Manufacturing-Chapter-Introduction.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
additive manufacturing of ss316l using mig welding
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Construction Project Organization Group 2.pptx

Basic regular expression, extended regular expression

  • 1. Introduction to Unix and OS Module 3 grep and Regular Expression Dr. Girisha G S Dept. of CSE SoE,DSU, Bengaluru 1
  • 2. Basic Regular Expression What is regular expression? - Regular expression is a pattern which contains meta characters for a complex search/match Classification of regular expression 1. Basic Regular Expression (BRE) 2. Extended Regular Expression (ERE) - grep command supports -> BRE by default -> ERE with the –E option - Basic Regular Expression make it possible to match a group of similar patterns
  • 3. The BRE character subset Meta character / character Matches * Zero or more occurrences of preceding expression E.g. a* matches no ‘a’, a, aa, aaa, ……. . A single character E.g. .* matches any number of characters [ ] Matches single character with in the brackets E.g. [pqr] matches a single character p, q, or r [1-4] matches digit between 1 & 3 ^ Match a pattern at the beginning of line E.g. ^The match any line which begin with the word The $ Match a pattern at the end of line E.g. ab$ matches any stream that end with b
  • 4. The character class ( [ ] ) - matches one of the character in the brackets E.g. To match Agarwal or agrawal, the RE is [Aa]g[ar][ra]wal The * Matches zero or more occurrences of the preceding expression E.g.1 g* matches single character g or any no of g’s i.e. no ‘g’, g, gg, ggg, ……. E.g.2 To match the pattern Agarwal, agrawal, aggarwal, the RE is [Aa]gg*[ar][ra]wal The Dot(.) - A dot(.) matches single character - .* sigifies any no of character or none E.g. Match all 4 character words beginning with 2 2…
  • 5. Specifying pattern location (^ & $) E.g. 1. Match lines starting with 2 ^2 E.g. 2 match lines where salary between no 7000 to 7999 7…$ grep & RE - You can also use regular expression with grep command Example1: search files Agarwal and agrawal from emp.lst $ grep ‘[aA]g[ar[ar]wal emp.lst Example2: search for the lines Agarwal, agrawal, aggarwal from emp.lst $ gepr ‘[aA]gg*[ar[ar]wal’ emp.lst Example3: print all lines that begin with letter ‘a ‘ followed by one character followed by letter sequence ‘ple’ from fruitlist.txt $ grep ‘^a.ple’ fruitlist.txt Example4: print all the lines ending with letter ‘e’ from sample.txt $ grep ‘e$’ sample.txt
  • 6. Extended Regular Expression - Extended RE make it possible to match disimilar patterns with a single expression - This set uses additional characters - grep command use them with –E option The ERE set + matches one or more occurrence of the previous character E.g a+ matches a,aa,aaa,…….. [ab]+ matches ab,abaab, ababab,….. [a-az]+ matches all string of lower characters ? Matches zero or q occurrence of preceding expression E.g. ab?c matches either ac or abc, here b is optiona | Indicates alternation E.g . (ab|cd) matches either ab or cd ( ) groups a series of REs together into a new RE
  • 7. grep and ERE - Use grep’s -E option to use ERE Example1: search for the lines Agarwal, aggarwal from the file emp.lst $ gepr -E ‘[aA]gg?arwal’ emp.lst Example2: search for the lines segupta and dasgupta from the file emp.lst $ grep –E ‘(sen|das)gupta’ emp.lst

Editor's Notes

  • #2: We use commands that filter data to select only the portion of data that we wish to view or operate on. t can be used to process information in powerful ways such as restructuring output to generate useful reports, modifying text in files and many other system administration tasks.
  • #3: If an expression uses metacharacters it is termed as regular exression
  • #5: The above RE matches two names This pattern matches 3 patterns