Basic HTML version of Foils prepared Sept 20 97

Foil 36 Regular Expressions -- Analogy with grep

From Tutorial on PERL Computational Science for Information Age Course CPS616 -- Sept 20 97. by Geoffrey C. Fox,Nancy McCracken,Tom Scavo


Regular expressions should be familiar as they are used in many UNIX commands with grep being the best known
  • % grep pattern file; # Print each line of a file containing pattern
Consider the simple pattern /Fox/. We can write the PERL version of grep as follows:
$line = 0;
while (<>) {
if ( /Fox/ ) {
  • print "$line: $_";
}
$line++;
}
Another familiar operator is s in sed (the batch or stream line editor) where
s/Pat1/Pat2/; # substitutes Pat1 by Pat2 in each line
We'll have more to say about the s operator later



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sun Sep 21 1997