SlideShare a Scribd company logo
1 of 33
Download to read offline
Linux For Embedded Systems
ForArabs
Ahmed ElArabawy
Course 102:
UnderstandingLinux
Lecture 13:
Regular Expressions
What is Regular Expressions ??
• We learned how to do simple text operations (like search on
single strings…)
• How about if I want to,
Search for string-1 or string-2 …
Search for a string only if it occurs at the beginning of the line
Search for a pattern (such as a phone number, email, URL,…)
Search for a pattern that have some repetition
etc...
• This means we need a more powerful mechanism to deal with
text patterns….
• That is the role of Regular Expressions (REGEX)
What is Regular Expressions ??
• We learned to use patterns with filenames via wild cards and
other expansions but those expansions were limited to file
names
• Regular expressions use similar techniques but used to general
text patterns (so don’t confuse the two, cause they have
different rules)
• Regular Expressions are a very powerful tool for creating text
patterns for use by several Linux tools (specially for search in
text)
• Used a lot in scripting languages and tools such as ‘perl’ and
‘python’
• One of the main tools using it is grep (the ‘re’ in ‘grep’ stands
for regular expressions)
Regex Are Complicated
• A lot of Special Cases and forms
• Some of the tools using it, have slight changes to the rules
• Some tools have richer set than the one used in bash (such as
perl)
• This lecture in only going to introduce the topic, if interested
there are a lot of sources on REGEX
grep Command
$ grep [options] <string/pattern> <files>
Search for the string or pattern within the set of files
Option Long Description
-i --ignore-case Ignore case
-v --invert-match Show lines that does not
match
-c --count Print only the count of the
matches
-l --files-with-matches Print only the filenames
-L --files-without-match
-n --line-number Include line numbers
-h --no-filename Don’t include the file name
Literal Vs. Meta-Characters
• Literal characters are those characters that represent
themselves in the search pattern
$ grep “error” *.log
The letters in “error” are all literal characters
• Meta characters are those characters that have special
meaning,
^ $ . [ ] { } - ? * + ( ) | 
All other characters are literal characters
Literal Vs. Meta-Characters
• Normally, it is recommended to put any expression with meta-
characters inside a quote, to avoid expansion according the
shell rules
• Meta characters can be treated as literals if they are escaped,
i.e. preceded by a back slash
• Examples, ^ { $ 
• The back slash can also convert some of the literal characters
into a meta-characters
• Examples : d w
• The regular expressions are patterns of text created from a
mix of literal characters and meta-characters
Types of Regular Expressions
• POSIX defines two types of regular expressions,
• Basic Regular Expressions (BRE)
• Extended Regular Expressions (ERE)
• Basic Regular Expressions use the following meta-characters, all
other characters are considered literal:
. ^ $ [ ] *
• Extended Regular Expressions use the following set in addition to
the basic set,
( ) { } ? + |
• Then the backslash is used to reverse those meta-characters into
literals (in ERE), and vice versa (in BRE)
• The tool ‘grep’ uses BRE
• To access ERE, use ‘egrep’ or ‘grep –E ‘
BASIC REGULAR EXPRESSIONS (BRE)
The “ANY” Character
(The dot character ‘ . ’ )
• The ‘.’ (dot) is a meta-character that represent any single
character (not including NULL character)
• For example,
$ grep “.zip” file.log
This searches for a 4 letter text pattern that starts with any
character followed by the three letters in ‘zip’
• May result in: gzip, bzip, gnubzip , rezipped
• But will not result in : zip
Anchor Characters
(^) and ($)
• The (^) at the beginning of the string means that this string
has to be at the beginning of the line
• The ($) at the end of the string means that this string has to be
at the end of the line
• Examples:
$ grep “^zip” file.txt  results in any line that starts with ‘zip’
$ grep “zip$” file.txt  results in any line that ends with ‘zip’
$ grep “^zip$” file.txt  results in any line that have only ‘zip’
$ grep “^$” file.txt  results in empty lines
Bracket Expressions
( [ ) and ( ] )
• The use of brackets for any of a set of characters listed between the
brackets
$ grep “[bg]zip” dict.txt
Results in: bzip, gzip, aabzip
• Any character inside the bracket will be considered literal except for
• (^) if it comes at the beginning (will considered as negation)
• (-) if it comes in the middle (will be considered as range)
• Negation,
$ grep “[^bg]zip” dict.txt
• Will catch words with any character before zip except b or g or Null
character
• Ranges
$ grep “[a-z]2” dict.txt
• Will catch words starting with any small letter followed by 2
$ grep “[a-fA-F]4” dict.txt
• Will catch any word with letter A-F (case insensitive) followed by 4
Shorthand Character Classes
• We can use a selected set of escaped characters to represent
some character classes
• Examples
regex Description
w Stands for [a-zA-Z0-9_] (word character)
s Stands for space characters or tabs or line breaks
t Stands for tabs(ASCII 0x09)
r Stands for Carriage Return (ASCII 0x0D)
n Stands for Line Feed (0x0A)
xnn Stands for character with ASCII = nn (xA9 == @)
Character Classes
• The following character classes can be used,
Class Description
[[:alnum:]] Alphanumeric [a-zA-Z0-9]
[[:word:]] same as alnum with addition of
underscore (w) [a-zA-Z0-9_]
[[:alpha:]] Only letters [a-zA-Z]
[[:digit:]] Only Digits [0-9]
[[:blank:]] Space Bar or Tab (s)
[[:lower:]] Only lower case letters [a-z]
[[:upper:]] Only upper case letters [A-Z]
[[:space:]] space
[[:xdigit:]] Hex digit [a-fA-F0-9]
EXTENDEDREGULAREXPRESSIONS(ERE)
Alternation
( | )
$ grep -E “AAA|BBB” file.txt
This matches any line containing AAA or BBB
• We separate the alternation from the rest of the regular
expression using ‘()’
$ grep –E “^(AAA|BBB|CCC)” file.txt
This matches any line starting with AAA or BBB or CCC
Quantifiers
(*,+, and ?)
• The character ‘?’ is used to express that the preceding
element to be optional (zero or one time)
• The character ‘*’ is used (zero or More times)
• The character ‘+’ is used (One or More times)
• Example,
We are searching any line starting with a phone number… this can
be in the format
(nnn) nnn – nnnn OR nnn nnn-nnnn
$ grep –E “^(?[0-9][0-9][0-9])? [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]” file.txt
• Example,
• Want to check which lines constitute a proper statement, start with a capital letter,
followed by any character or spaces, and ending by a period
$ grep –E “^[[:upper:]][[:upper:][:lower:] ]*.$” file.txt
Matching Count
( { ) and ( } )
• The curly brackets ‘{}’ can be used to match an element
specific number of times
• Example: The phone number example:
$ egrep “(?[:digit:]{3})?[:digit:]{3}-[:digit:]{4}” file.txt
Matching Count Description
{n} n times
{n,m} n to m times (inclusive)
{n,} n or more times
{,m} m or less times
Examples
• Some example to play with:
$ egrep -i ‘Continued.*’ file.txt
$ grep “[0-9a-fxA-FX]abc” file.txt
$ egrep ‘fish|chips|pies’ file.txt
$ egrep -i ‘(cream|fish|birthday|) cakes’ file.txt
$ grep ‘[Jj]oe [Bb]loggs’ file.txt
$ grep -E “colou?r” file.txt
Using Regular Expressions
in Other Commands
• A lot of other commands also work with regular expressions
such as,
• The commands ‘find’ and ‘locate’ for finding files
• The ‘vi’ editor
• The command ‘less’ can perform text search using regular
expressions
• Tools that use regular expressions extensively
• sed
• awk
sed & awk TOOLS
sed
• sed stands for Stream Editor
• sed is used to modify text files within a program, script, or
from the command line
• sed is one of the very famous programs to use regular
expressions extensively
• sed is a rich program, and has many options, so we are just
providing a quick introduction for it in this lecture
• We will discuss some of its famous patterns, and usage
Syntax
• All sed commands are formed into a command string that is
passed to sed in the following format
$ sed ‘command string’
• The input file that sed works on can be passed through
• Input redirection
$ sed ‘command string’ < input-file
• Pipes
$ cat input-file | sed ‘command string’
• The output goes to stdout, in case you want the output in a
file, you need to redirect it
$ sed ‘command string’ < input-file > output-file
Substituting Text
(The ‘s’ Command)
• Substituting text is one of the most popular commands of sed
• It searches for all the occurrences of a certain text pattern
(using REGEX) and substitute it with different string
• Example:
$ sed ‘s/day/night/’ <old >new
Run the sed tool
Substitution Command
Pattern to Search for
Substitute with
Input file
Output file
Selecting the delimiter Character
• By default, sed uses the slash as a separator (delimiter)
between the command, search pattern, and the replacement
pattern.
• If we need to use a slash inside the search pattern or the
replacement pattern, we must escape it (precede it with a
back slash)
$ sed ‘s/yes/no/true/false/’ < old-file > new-file
• Another option is to use another character as a separator,
$ sed ‘s:yes/no:true/false:’ < old-file > new-file
$ sed ‘s|yes/no|true/false|’ < old-file > new-file
Using the Global Option
• sed works on the file line by line
• So this command
$ sed ‘s/day/night/’ <old-file >new-file
• Changes one occurrence of the search pattern (day) in each line
into the replacement pattern (night)
• If the search pattern exists multiple times inside the same line,
only the first occurrence will be substituted
• To avoid that use the global flag
$ sed ‘s/day/night/g’ <old-file >new-file
Using the Matched String
(&)
• Suppose that we want to search for all occurrences of a string,
and want to put it inside brackets. We would use the
command
$ sed ‘s/abc/(abc)/g’ <old-file >new-file
• What if we want to use a pattern for our search , then we can
use the meta-character & to indicate the matched string
$ sed ‘s/gr[ae]y/(&)/g’ <old-file >new-file
$ sed -r ‘s/[0-9]+/(&)/g’ <old-file >new-file
• Another example, let us say we want to repeat any number in
the file
$ sed -r ‘s/[0-9]+/& &/g’ <old-file > new-file
Note, the last examples have been using ‘-r’ to support Extended
REGEX
awk
• awk is an excellent tool for processing files containing
tabulated data (in rows and columns)
• awk also has strong string manipulation functions, so it can
search for particular strings and modify the output
• awk also has support for associative arrays, which are very
• There is a GNU version of AWK called "gawk“
• The name “awk” comes from the names of its inventors
Syntax
• AWK reads the input file line by line
• On each line, it performs a test based on some pattern
• If the test succeeds it performs an action
• Thus an awk statement would look like:
pattern { action }
• Some basic pattern,
• No Pattern: perform the action on every line
• BEGIN: perform the action if this is the beginning of the file
• END: perform the action if this is the end of the file
• Example:
BEGIN { print “Now we will start the file" }
{ print }
END { print “That was the end of the file" }
More Advanced AWK scripts
BEGIN { print "FiletOwner"}
{ print $9, "t", $3}
END { print " - DONE -" }
• This will do the following ,
• When the first line of the input file is read, the scripts outputs the
strings “File” and “Owner” separated by a TAB
• For every line read from the input file (including the first one, the
data in the 9th column and that of the 3rd column are printed
separated by a TAB
• After the input file is fully read, the scripts prints “- DONE –”
• Now if you apply this awk statements on the output of
$ ls -l
It should make a list of file names and owner name for all files
More on AWK
• AWK has a very powerful syntax
• It can be written inside a bash script
• It can define variable, perform mathematical operations
• It also have a strong support for REGEX
http://Linux4EmbeddedSystems.com

More Related Content

What's hot

Course 102: Lecture 2: Unwrapping Linux
Course 102: Lecture 2: Unwrapping Linux Course 102: Lecture 2: Unwrapping Linux
Course 102: Lecture 2: Unwrapping Linux Ahmed El-Arabawy
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Course 102: Lecture 3: Basic Concepts And Commands
Course 102: Lecture 3: Basic Concepts And Commands Course 102: Lecture 3: Basic Concepts And Commands
Course 102: Lecture 3: Basic Concepts And Commands Ahmed El-Arabawy
 
Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell Ahmed El-Arabawy
 
Course 102: Lecture 4: Using Wild Cards
Course 102: Lecture 4: Using Wild CardsCourse 102: Lecture 4: Using Wild Cards
Course 102: Lecture 4: Using Wild CardsAhmed El-Arabawy
 
Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling Ahmed El-Arabawy
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scriptingvceder
 
Course 102: Lecture 17: Process Monitoring
Course 102: Lecture 17: Process Monitoring Course 102: Lecture 17: Process Monitoring
Course 102: Lecture 17: Process Monitoring Ahmed El-Arabawy
 
Course 102: Lecture 27: FileSystems in Linux (Part 2)
Course 102: Lecture 27: FileSystems in Linux (Part 2)Course 102: Lecture 27: FileSystems in Linux (Part 2)
Course 102: Lecture 27: FileSystems in Linux (Part 2)Ahmed El-Arabawy
 
Course 102: Lecture 18: Process Life Cycle
Course 102: Lecture 18: Process Life CycleCourse 102: Lecture 18: Process Life Cycle
Course 102: Lecture 18: Process Life CycleAhmed El-Arabawy
 
Linux standard file system
Linux standard file systemLinux standard file system
Linux standard file systemTaaanu01
 
Introduction To Power Shell
Introduction To Power ShellIntroduction To Power Shell
Introduction To Power ShellIvan Suhinin
 
Sed & awk the dynamic duo
Sed & awk   the dynamic duoSed & awk   the dynamic duo
Sed & awk the dynamic duoJoshua Thijssen
 
Introduction to Linux
Introduction to Linux Introduction to Linux
Introduction to Linux Harish R
 
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)Ahmed El-Arabawy
 
Course 102: Lecture 14: Users and Permissions
Course 102: Lecture 14: Users and PermissionsCourse 102: Lecture 14: Users and Permissions
Course 102: Lecture 14: Users and PermissionsAhmed El-Arabawy
 
Oracle forms and reports 11g installation on linux
Oracle forms and reports 11g installation on linuxOracle forms and reports 11g installation on linux
Oracle forms and reports 11g installation on linuxVenu Palakolanu
 

What's hot (20)

Course 102: Lecture 2: Unwrapping Linux
Course 102: Lecture 2: Unwrapping Linux Course 102: Lecture 2: Unwrapping Linux
Course 102: Lecture 2: Unwrapping Linux
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Course 102: Lecture 3: Basic Concepts And Commands
Course 102: Lecture 3: Basic Concepts And Commands Course 102: Lecture 3: Basic Concepts And Commands
Course 102: Lecture 3: Basic Concepts And Commands
 
Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell
 
Course 102: Lecture 4: Using Wild Cards
Course 102: Lecture 4: Using Wild CardsCourse 102: Lecture 4: Using Wild Cards
Course 102: Lecture 4: Using Wild Cards
 
Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
 
Course 102: Lecture 17: Process Monitoring
Course 102: Lecture 17: Process Monitoring Course 102: Lecture 17: Process Monitoring
Course 102: Lecture 17: Process Monitoring
 
Course 102: Lecture 27: FileSystems in Linux (Part 2)
Course 102: Lecture 27: FileSystems in Linux (Part 2)Course 102: Lecture 27: FileSystems in Linux (Part 2)
Course 102: Lecture 27: FileSystems in Linux (Part 2)
 
Linux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell ScriptingLinux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell Scripting
 
Course 102: Lecture 18: Process Life Cycle
Course 102: Lecture 18: Process Life CycleCourse 102: Lecture 18: Process Life Cycle
Course 102: Lecture 18: Process Life Cycle
 
Linux standard file system
Linux standard file systemLinux standard file system
Linux standard file system
 
Introduction To Power Shell
Introduction To Power ShellIntroduction To Power Shell
Introduction To Power Shell
 
Sed & awk the dynamic duo
Sed & awk   the dynamic duoSed & awk   the dynamic duo
Sed & awk the dynamic duo
 
Introduction to Linux
Introduction to Linux Introduction to Linux
Introduction to Linux
 
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)
 
Course 102: Lecture 14: Users and Permissions
Course 102: Lecture 14: Users and PermissionsCourse 102: Lecture 14: Users and Permissions
Course 102: Lecture 14: Users and Permissions
 
Oracle forms and reports 11g installation on linux
Oracle forms and reports 11g installation on linuxOracle forms and reports 11g installation on linux
Oracle forms and reports 11g installation on linux
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
From Zero to Docker
From Zero to DockerFrom Zero to Docker
From Zero to Docker
 

Viewers also liked

Course 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment VariablesCourse 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment VariablesAhmed El-Arabawy
 
Course 101: Lecture 4: A Tour in RTOS Land
Course 101: Lecture 4: A Tour in RTOS Land Course 101: Lecture 4: A Tour in RTOS Land
Course 101: Lecture 4: A Tour in RTOS Land Ahmed El-Arabawy
 
Course 101: Lecture 2: Introduction to Operating Systems
Course 101: Lecture 2: Introduction to Operating Systems Course 101: Lecture 2: Introduction to Operating Systems
Course 101: Lecture 2: Introduction to Operating Systems Ahmed El-Arabawy
 
Course 101: Lecture 6: Installing Ubuntu
Course 101: Lecture 6: Installing Ubuntu Course 101: Lecture 6: Installing Ubuntu
Course 101: Lecture 6: Installing Ubuntu Ahmed El-Arabawy
 
Course 101: Lecture 5: Linux & GNU
Course 101: Lecture 5: Linux & GNU Course 101: Lecture 5: Linux & GNU
Course 101: Lecture 5: Linux & GNU Ahmed El-Arabawy
 
Course 101: Lecture 1: Introduction to Embedded Systems
Course 101: Lecture 1: Introduction to Embedded SystemsCourse 101: Lecture 1: Introduction to Embedded Systems
Course 101: Lecture 1: Introduction to Embedded SystemsAhmed El-Arabawy
 
Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers Ahmed El-Arabawy
 
Course 102: Lecture 20: Networking In Linux (Basic Concepts)
Course 102: Lecture 20: Networking In Linux (Basic Concepts) Course 102: Lecture 20: Networking In Linux (Basic Concepts)
Course 102: Lecture 20: Networking In Linux (Basic Concepts) Ahmed El-Arabawy
 
Course 102: Lecture 22: Package Management
Course 102: Lecture 22: Package Management Course 102: Lecture 22: Package Management
Course 102: Lecture 22: Package Management Ahmed El-Arabawy
 
Embedded Systems: Lecture 4: Selecting the Proper RTOS
Embedded Systems: Lecture 4: Selecting the Proper RTOSEmbedded Systems: Lecture 4: Selecting the Proper RTOS
Embedded Systems: Lecture 4: Selecting the Proper RTOSAhmed El-Arabawy
 
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi APEmbedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi APAhmed El-Arabawy
 
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry PiEmbedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry PiAhmed El-Arabawy
 
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)Ahmed El-Arabawy
 

Viewers also liked (13)

Course 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment VariablesCourse 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment Variables
 
Course 101: Lecture 4: A Tour in RTOS Land
Course 101: Lecture 4: A Tour in RTOS Land Course 101: Lecture 4: A Tour in RTOS Land
Course 101: Lecture 4: A Tour in RTOS Land
 
Course 101: Lecture 2: Introduction to Operating Systems
Course 101: Lecture 2: Introduction to Operating Systems Course 101: Lecture 2: Introduction to Operating Systems
Course 101: Lecture 2: Introduction to Operating Systems
 
Course 101: Lecture 6: Installing Ubuntu
Course 101: Lecture 6: Installing Ubuntu Course 101: Lecture 6: Installing Ubuntu
Course 101: Lecture 6: Installing Ubuntu
 
Course 101: Lecture 5: Linux & GNU
Course 101: Lecture 5: Linux & GNU Course 101: Lecture 5: Linux & GNU
Course 101: Lecture 5: Linux & GNU
 
Course 101: Lecture 1: Introduction to Embedded Systems
Course 101: Lecture 1: Introduction to Embedded SystemsCourse 101: Lecture 1: Introduction to Embedded Systems
Course 101: Lecture 1: Introduction to Embedded Systems
 
Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers
 
Course 102: Lecture 20: Networking In Linux (Basic Concepts)
Course 102: Lecture 20: Networking In Linux (Basic Concepts) Course 102: Lecture 20: Networking In Linux (Basic Concepts)
Course 102: Lecture 20: Networking In Linux (Basic Concepts)
 
Course 102: Lecture 22: Package Management
Course 102: Lecture 22: Package Management Course 102: Lecture 22: Package Management
Course 102: Lecture 22: Package Management
 
Embedded Systems: Lecture 4: Selecting the Proper RTOS
Embedded Systems: Lecture 4: Selecting the Proper RTOSEmbedded Systems: Lecture 4: Selecting the Proper RTOS
Embedded Systems: Lecture 4: Selecting the Proper RTOS
 
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi APEmbedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
 
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry PiEmbedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
 
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
 

Similar to Course 102: Lecture 13: Regular Expressions

Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrepTri Truong
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeProf. Wim Van Criekinge
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Andrea Telatin
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex powerMax Kleiner
 
Python_Unit_III.pptx
Python_Unit_III.pptxPython_Unit_III.pptx
Python_Unit_III.pptxssuserc755f1
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressionsAcácio Oliveira
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressionsmussawir20
 
Unit 8 text processing tools
Unit 8 text processing toolsUnit 8 text processing tools
Unit 8 text processing toolsroot_fibo
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular ExpressionChapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expressionazzamhadeel89
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RHellen Gakuruh
 
Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017Sandy Smith
 
Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5bilcorry
 
Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Sandy Smith
 
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Sandy Smith
 

Similar to Course 102: Lecture 13: Regular Expressions (20)

Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
 
Bioinformatica p2-p3-introduction
Bioinformatica p2-p3-introductionBioinformatica p2-p3-introduction
Bioinformatica p2-p3-introduction
 
Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
 
Lecture_4.pdf
Lecture_4.pdfLecture_4.pdf
Lecture_4.pdf
 
Spsl II unit
Spsl   II unitSpsl   II unit
Spsl II unit
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex power
 
Python_Unit_III.pptx
Python_Unit_III.pptxPython_Unit_III.pptx
Python_Unit_III.pptx
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
 
Regex posix
Regex posixRegex posix
Regex posix
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressions
 
Unit 8 text processing tools
Unit 8 text processing toolsUnit 8 text processing tools
Unit 8 text processing tools
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular ExpressionChapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expression
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
 
Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017
 
Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5
 
Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014
 
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15
 

Recently uploaded

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaWSO2
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingWSO2
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceIES VE
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 

Recently uploaded (20)

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 

Course 102: Lecture 13: Regular Expressions

  • 1. Linux For Embedded Systems ForArabs Ahmed ElArabawy Course 102: UnderstandingLinux
  • 3. What is Regular Expressions ?? • We learned how to do simple text operations (like search on single strings…) • How about if I want to, Search for string-1 or string-2 … Search for a string only if it occurs at the beginning of the line Search for a pattern (such as a phone number, email, URL,…) Search for a pattern that have some repetition etc... • This means we need a more powerful mechanism to deal with text patterns…. • That is the role of Regular Expressions (REGEX)
  • 4. What is Regular Expressions ?? • We learned to use patterns with filenames via wild cards and other expansions but those expansions were limited to file names • Regular expressions use similar techniques but used to general text patterns (so don’t confuse the two, cause they have different rules) • Regular Expressions are a very powerful tool for creating text patterns for use by several Linux tools (specially for search in text) • Used a lot in scripting languages and tools such as ‘perl’ and ‘python’ • One of the main tools using it is grep (the ‘re’ in ‘grep’ stands for regular expressions)
  • 5. Regex Are Complicated • A lot of Special Cases and forms • Some of the tools using it, have slight changes to the rules • Some tools have richer set than the one used in bash (such as perl) • This lecture in only going to introduce the topic, if interested there are a lot of sources on REGEX
  • 6. grep Command $ grep [options] <string/pattern> <files> Search for the string or pattern within the set of files Option Long Description -i --ignore-case Ignore case -v --invert-match Show lines that does not match -c --count Print only the count of the matches -l --files-with-matches Print only the filenames -L --files-without-match -n --line-number Include line numbers -h --no-filename Don’t include the file name
  • 7. Literal Vs. Meta-Characters • Literal characters are those characters that represent themselves in the search pattern $ grep “error” *.log The letters in “error” are all literal characters • Meta characters are those characters that have special meaning, ^ $ . [ ] { } - ? * + ( ) | All other characters are literal characters
  • 8. Literal Vs. Meta-Characters • Normally, it is recommended to put any expression with meta- characters inside a quote, to avoid expansion according the shell rules • Meta characters can be treated as literals if they are escaped, i.e. preceded by a back slash • Examples, ^ { $ • The back slash can also convert some of the literal characters into a meta-characters • Examples : d w • The regular expressions are patterns of text created from a mix of literal characters and meta-characters
  • 9. Types of Regular Expressions • POSIX defines two types of regular expressions, • Basic Regular Expressions (BRE) • Extended Regular Expressions (ERE) • Basic Regular Expressions use the following meta-characters, all other characters are considered literal: . ^ $ [ ] * • Extended Regular Expressions use the following set in addition to the basic set, ( ) { } ? + | • Then the backslash is used to reverse those meta-characters into literals (in ERE), and vice versa (in BRE) • The tool ‘grep’ uses BRE • To access ERE, use ‘egrep’ or ‘grep –E ‘
  • 11. The “ANY” Character (The dot character ‘ . ’ ) • The ‘.’ (dot) is a meta-character that represent any single character (not including NULL character) • For example, $ grep “.zip” file.log This searches for a 4 letter text pattern that starts with any character followed by the three letters in ‘zip’ • May result in: gzip, bzip, gnubzip , rezipped • But will not result in : zip
  • 12. Anchor Characters (^) and ($) • The (^) at the beginning of the string means that this string has to be at the beginning of the line • The ($) at the end of the string means that this string has to be at the end of the line • Examples: $ grep “^zip” file.txt  results in any line that starts with ‘zip’ $ grep “zip$” file.txt  results in any line that ends with ‘zip’ $ grep “^zip$” file.txt  results in any line that have only ‘zip’ $ grep “^$” file.txt  results in empty lines
  • 13. Bracket Expressions ( [ ) and ( ] ) • The use of brackets for any of a set of characters listed between the brackets $ grep “[bg]zip” dict.txt Results in: bzip, gzip, aabzip • Any character inside the bracket will be considered literal except for • (^) if it comes at the beginning (will considered as negation) • (-) if it comes in the middle (will be considered as range) • Negation, $ grep “[^bg]zip” dict.txt • Will catch words with any character before zip except b or g or Null character • Ranges $ grep “[a-z]2” dict.txt • Will catch words starting with any small letter followed by 2 $ grep “[a-fA-F]4” dict.txt • Will catch any word with letter A-F (case insensitive) followed by 4
  • 14. Shorthand Character Classes • We can use a selected set of escaped characters to represent some character classes • Examples regex Description w Stands for [a-zA-Z0-9_] (word character) s Stands for space characters or tabs or line breaks t Stands for tabs(ASCII 0x09) r Stands for Carriage Return (ASCII 0x0D) n Stands for Line Feed (0x0A) xnn Stands for character with ASCII = nn (xA9 == @)
  • 15. Character Classes • The following character classes can be used, Class Description [[:alnum:]] Alphanumeric [a-zA-Z0-9] [[:word:]] same as alnum with addition of underscore (w) [a-zA-Z0-9_] [[:alpha:]] Only letters [a-zA-Z] [[:digit:]] Only Digits [0-9] [[:blank:]] Space Bar or Tab (s) [[:lower:]] Only lower case letters [a-z] [[:upper:]] Only upper case letters [A-Z] [[:space:]] space [[:xdigit:]] Hex digit [a-fA-F0-9]
  • 17. Alternation ( | ) $ grep -E “AAA|BBB” file.txt This matches any line containing AAA or BBB • We separate the alternation from the rest of the regular expression using ‘()’ $ grep –E “^(AAA|BBB|CCC)” file.txt This matches any line starting with AAA or BBB or CCC
  • 18. Quantifiers (*,+, and ?) • The character ‘?’ is used to express that the preceding element to be optional (zero or one time) • The character ‘*’ is used (zero or More times) • The character ‘+’ is used (One or More times) • Example, We are searching any line starting with a phone number… this can be in the format (nnn) nnn – nnnn OR nnn nnn-nnnn $ grep –E “^(?[0-9][0-9][0-9])? [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]” file.txt • Example, • Want to check which lines constitute a proper statement, start with a capital letter, followed by any character or spaces, and ending by a period $ grep –E “^[[:upper:]][[:upper:][:lower:] ]*.$” file.txt
  • 19. Matching Count ( { ) and ( } ) • The curly brackets ‘{}’ can be used to match an element specific number of times • Example: The phone number example: $ egrep “(?[:digit:]{3})?[:digit:]{3}-[:digit:]{4}” file.txt Matching Count Description {n} n times {n,m} n to m times (inclusive) {n,} n or more times {,m} m or less times
  • 20. Examples • Some example to play with: $ egrep -i ‘Continued.*’ file.txt $ grep “[0-9a-fxA-FX]abc” file.txt $ egrep ‘fish|chips|pies’ file.txt $ egrep -i ‘(cream|fish|birthday|) cakes’ file.txt $ grep ‘[Jj]oe [Bb]loggs’ file.txt $ grep -E “colou?r” file.txt
  • 21. Using Regular Expressions in Other Commands • A lot of other commands also work with regular expressions such as, • The commands ‘find’ and ‘locate’ for finding files • The ‘vi’ editor • The command ‘less’ can perform text search using regular expressions • Tools that use regular expressions extensively • sed • awk
  • 22. sed & awk TOOLS
  • 23. sed • sed stands for Stream Editor • sed is used to modify text files within a program, script, or from the command line • sed is one of the very famous programs to use regular expressions extensively • sed is a rich program, and has many options, so we are just providing a quick introduction for it in this lecture • We will discuss some of its famous patterns, and usage
  • 24. Syntax • All sed commands are formed into a command string that is passed to sed in the following format $ sed ‘command string’ • The input file that sed works on can be passed through • Input redirection $ sed ‘command string’ < input-file • Pipes $ cat input-file | sed ‘command string’ • The output goes to stdout, in case you want the output in a file, you need to redirect it $ sed ‘command string’ < input-file > output-file
  • 25. Substituting Text (The ‘s’ Command) • Substituting text is one of the most popular commands of sed • It searches for all the occurrences of a certain text pattern (using REGEX) and substitute it with different string • Example: $ sed ‘s/day/night/’ <old >new Run the sed tool Substitution Command Pattern to Search for Substitute with Input file Output file
  • 26. Selecting the delimiter Character • By default, sed uses the slash as a separator (delimiter) between the command, search pattern, and the replacement pattern. • If we need to use a slash inside the search pattern or the replacement pattern, we must escape it (precede it with a back slash) $ sed ‘s/yes/no/true/false/’ < old-file > new-file • Another option is to use another character as a separator, $ sed ‘s:yes/no:true/false:’ < old-file > new-file $ sed ‘s|yes/no|true/false|’ < old-file > new-file
  • 27. Using the Global Option • sed works on the file line by line • So this command $ sed ‘s/day/night/’ <old-file >new-file • Changes one occurrence of the search pattern (day) in each line into the replacement pattern (night) • If the search pattern exists multiple times inside the same line, only the first occurrence will be substituted • To avoid that use the global flag $ sed ‘s/day/night/g’ <old-file >new-file
  • 28. Using the Matched String (&) • Suppose that we want to search for all occurrences of a string, and want to put it inside brackets. We would use the command $ sed ‘s/abc/(abc)/g’ <old-file >new-file • What if we want to use a pattern for our search , then we can use the meta-character & to indicate the matched string $ sed ‘s/gr[ae]y/(&)/g’ <old-file >new-file $ sed -r ‘s/[0-9]+/(&)/g’ <old-file >new-file • Another example, let us say we want to repeat any number in the file $ sed -r ‘s/[0-9]+/& &/g’ <old-file > new-file Note, the last examples have been using ‘-r’ to support Extended REGEX
  • 29. awk • awk is an excellent tool for processing files containing tabulated data (in rows and columns) • awk also has strong string manipulation functions, so it can search for particular strings and modify the output • awk also has support for associative arrays, which are very • There is a GNU version of AWK called "gawk“ • The name “awk” comes from the names of its inventors
  • 30. Syntax • AWK reads the input file line by line • On each line, it performs a test based on some pattern • If the test succeeds it performs an action • Thus an awk statement would look like: pattern { action } • Some basic pattern, • No Pattern: perform the action on every line • BEGIN: perform the action if this is the beginning of the file • END: perform the action if this is the end of the file • Example: BEGIN { print “Now we will start the file" } { print } END { print “That was the end of the file" }
  • 31. More Advanced AWK scripts BEGIN { print "FiletOwner"} { print $9, "t", $3} END { print " - DONE -" } • This will do the following , • When the first line of the input file is read, the scripts outputs the strings “File” and “Owner” separated by a TAB • For every line read from the input file (including the first one, the data in the 9th column and that of the 3rd column are printed separated by a TAB • After the input file is fully read, the scripts prints “- DONE –” • Now if you apply this awk statements on the output of $ ls -l It should make a list of file names and owner name for all files
  • 32. More on AWK • AWK has a very powerful syntax • It can be written inside a bash script • It can define variable, perform mathematical operations • It also have a strong support for REGEX