130-Linux Shell Scripting
130-Linux Shell Scripting
Session 12
The biggest problem with using regular expressions is that there isn't
just one set of them. Several different applications use different types
of regular expressions in the Linux environment.
Plain text
$ echo "This is a test" | sed -n '/test/p'
This is a test
patterns are case sensitive. This means they'll only match patterns
with the proper case of characters.
Regular
Expressions
Special characters
.*[]^${}\+?|()
Regular
Expressions
For example, if you want to search for a dollar sign in your text, just
precede it with a backslash character:
$ cat data2
The cost is $4.00
The caret character (^) defines a pattern that starts at the beginning
of a line of text in the data stream. If the pattern is located any place
other than the start of the line of text, the regular expression pattern
fails.
To use the caret character, you must place it before the pattern
specified in the regular expression:
$ echo "The book store" | sed -n '/^book/p'
$
$ echo "Books are great" | sed -n '/^Book/p'
Books are great
$
Regular
Expressions
Looking for the ending
The dot special character is used to match any single character except
a newline character. The dot character must match a character
though; if there's no character in the place of the dot, then the
pattern will fail.
Regular
Expressions
$ cat data6
This is a test of a line.
The cat is sleeping.
That is a very nice hat.
This test is at line four.
at ten o'clock we'll go home.
$ sed -n '/.at/p' data6
The cat is sleeping.
That is a very nice hat.
This test is at line four.
$
Regular
Expressions
Character classes
The dot special character is great for matching a character position
against any character, but what if you want to limit what characters to
match? This is called a character class in regular expressions.
To define a character class, you use square brackets. The brackets
should contain any character that you want to include in the class.
Write a shell script that counts the executable files that are present
in the directories defined in your PATH environment variable.
Regular
Expressions
#!/bin/bash
# count number of files in your PATH
mypath=`echo $PATH | sed 's/:/ /g'`
count=0
for directory in $mypath
do
check=`ls $directory`
for item in $check
do
count=$[ $count + 1 ]
done
echo "$directory - $count"
count=0
done