The UNIX and Linux Forums  

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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extracting a string from one file and searching the same string in other files mohancrr Shell Programming and Scripting 1 09-19-2007 04:17 AM
Perl: searching for a string in a file... pondlife Shell Programming and Scripting 4 09-17-2007 08:35 AM
Hi! Searching for a text string in UNIX skwadim UNIX for Dummies Questions & Answers 1 12-22-2006 01:13 PM
Searching directory for file that contains some text. LordJezo UNIX for Dummies Questions & Answers 4 04-04-2006 09:58 PM
Help with searching a text file thekid2 Shell Programming and Scripting 6 01-02-2004 02:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (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: 717
Hi.
Quote:
Originally Posted by meevagh View Post
... I prob could have explained it a bit better. ...
Changing 4 characters in the regular expression allows:

Code:
% ./p3 data1 data2 data3

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

 Oh, a miss!

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

 Hit!

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

 Oh, a miss!

Best wishes ... cheers, drl
  #2 (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: 717
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
Closed Thread

Bookmarks

Tags
linux, perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0