Basic HTML version of Foils prepared March 30 97

Foil 35 Regular Expressions -- Analogy with grep
(Chapter 7 of the Llama Book)

From PERL4 Tutorial for CPS616 Computational Science for Information Age Course CPS616 -- February 1995. by Geoffrey C. Fox


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 $_

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 Sat Sep 6 1997