4.1 Searching for a Pattern in Files

4.1  Searching for a Pattern in Files

  We will start by writing a simple program that takes as its first argument a pattern. It takes one or more additional arguments to specify one or more files where it looks for the pattern. If it finds a pattern, it prints the line where the pattern is found, preceded by the name of the file.

 Program 4.1

#!/usr/bin/perl 
$pattern = shift(@ARGV);

while (<>){
    if (/$pattern/){
        print "\nFile $ARGV:";
        print "\t$_";     #prints the line
    }
}

Assuming the program is stored in the file grepfile.pl, an example call is the following.

%grepfile.pl theoretical *.tex

Here, the first argument given to the script istheoretical.theoretical is considered a pattern. The second argument given to the script is*.tex. It expands to a list that contains the names of all files in the current directory that end with.tex. So, the script looks for the pattern theoretical in all such files. It is a very simple pattern that happens to be a sequence of alphabetic characters.

The script is given the pattern and the names of all the files as one single list called@ARGV. The script removes the first element of this list and calls it $pattern.The names of the files remain in @ARGV. It then uses the angle bracket input construct inside a while loop to read each line from each of the files whose names are in @ARGV. It looks for $pattern in each line it scans and if it succeeds, it prints the name of the file followed by the line containing $pattern. The special variable $ARGV contains the name of the current file from @ARGV that is being processed. In this specific call,$pattern has the value theoretical and therefore, the script prints all lines containing theoretical of all files in the current directory whose names end with tex. The output looks like the following:

File nature.tex:        theoretically satisfying, will be
File nature.tex:        operators to compose actions which are more complex. In 
                                                     theoretical
File whole.tex:         discuss theoretical and practical details of the components in 
File whole.tex:         Indeed, many (though not all) theoretical 
                                                     discussions of semantic
File whole.tex:         place this on a solid theoretical foundation, and we 
                                                     expect it to offer
File whole.tex:         from a theoretical perspective, we claim that the 
                                                     animations produced
File whole.tex:         more rigorous theoretical foundation.

In the output single lines have been broken into two to fit the printed page.

We should note here that when we specify a pattern inside forward slashes, we can use variables inside the pattern. In the example given above, the pattern is specified solely in terms of the scalar variable
$pattern. The variable $pattern as in /$pattern/ is first interpolated for its value before pattern matching starts. In this respect, the delimiting forward slashes (/) behave like double quotes (").