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


1 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
2 Consider the simple pattern /Fox/. We can write the PERL version of grep as follows:
3 $line = 0;
4 while (<>) {
5 if ( /Fox/ ) {
  • print "$line: $_";
6 }
7 $line++;
8 }
9 Another familiar operator is s in sed (the batch or stream line editor) where
10 s/Pat1/Pat2/; # substitutes Pat1 by Pat2 in each line
11 We'll have more to say about the s operator later

in Table To:


© 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