The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #6 (permalink)  
Old 04-28-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 712
Hi.

Here is a quickly-written possibility:
Code:
#!/usr/bin/perl

# @(#) p2       Demonstrate matching across line boundaries.

use warnings;
use strict;

my ($debug);
$debug = 0;
$debug = 1;

my $file;

for $file (@ARGV) {
  print "\n -----\n";
  my $lines = slurp($file);
  print " File contains:\n$lines";

  print "\n";
  if ( $lines =~ /United.*Champions.*Ronaldo/xms ) {
    print " Hit!\n";
  }
  else {
    print " Oh, a miss!\n";
  }
}

sub slurp {

  # Best practices, p213 for a file.
  my ($file) = shift;
  my ($f);
  open( $f, "<", $file ) || die " Can't open file $file, quitting.\n";
  my $scalar = do { local $/; <$f> };
  return $scalar;
}

exit(0);
Producing output for a bad dataset and a good dataset:
Code:
% ./p2 data1 data2

 -----
 File contains:
United
Champions
Liverpool
Losers
Torres

 Oh, a miss!

 -----
 File contains:
United
Champions
Ronaldo
Liverpool
Losers
Torres

 Hit!
See perl documentation for details ... cheers, drl