1 |
Regular expressions should be familiar as they are used in many UNIX commands with grep as best known
-
grep pattern file; # Prints out each line of file containing pattern
|
2 |
The rules for pattern are rich and we will discuss later -- consider here the simple pattern Fox
|
3 |
Then we can write the PERL version of grep as follows:
|
4 |
$line =0;
|
5 |
while (<>) {
-
if( /Fox/ ) { # Generalize to /Pattern/ to test positive if Pattern in $_
-
print $line, "$_"; } # $_ is current line by default
-
$line++; }
|
6 |
Another familiar operator should be s in sed (the batch or stream line editor) where
|
7 |
s/Pattern1/Pattern2/; # substitutes Pattern1 by Pattern2 in each line
|
8 |
The same command can be used in PERL with again substitution occuring on $_
|