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
Search text from a file and print text and one previous line too kamranjalal Shell Programming and Scripting 6 01-06-2009 03:27 AM
Perl: Match a line with multiple search patterns Juha Shell Programming and Scripting 10 04-09-2008 02:43 AM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
How to cut multiple patterns from a file? Vijay06 Shell Programming and Scripting 5 11-26-2007 09:38 PM
How to parameterize multiple search patterns and generate a new file augustinep UNIX for Dummies Questions & Answers 6 07-30-2003 09:50 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 03-25-2009
Sp3ck Sp3ck is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 2
Perl - How to search a text file with multiple patterns?

Good day, great gurus,

I'm new to Perl, and programming in general. I'm trying to retrieve a column of data from my text file which spans a non-specific number of lines. So I did a regexp that will pick out the columns. However,my pattern would vary. I tried using a foreach loop unsuccessfully. How would I thus go about creating a script in perl that would:
1) Run a list of patterns (names in this case)
2) A pattern search of each in a file

The code I managed so far is this :


Code:
!/usr/bin/perl
use warnings;
use strict;



my @identity = qw/Joseph Clark May/;


my $data_file = 'C:\tests\Profiles.txt';
open (DAT, $data_file);

foreach $name(@identity) {
    while (<DAT>) {
        print if (/$name/ .. /\/\//);
    }
}

    

close (DAT);



So a data file like this:
NAME Joseph
blah
blah
blah
//
NAME Kent
blah
blah
blah
blah
//
NAME Clark
blah
blah
//
...


would give me this (after running the script):

Code:
NAME Joseph
blah
blah
blah
//
NAME Clark
 blah
 blah
 blah
blah
 //
NAME May
blah
blah
blah
//

above is just an example, but the file is huge, and so I just need to filter out these data. Any ideas much appreciated

Oh, and I only get one result, which is Joseph's data....

Last edited by Yogesh Sawant; 03-25-2009 at 05:45 AM.. Reason: added code tags
  #2 (permalink)  
Old 03-25-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731

Code:
!/usr/bin/perl
use warnings;
use strict;

use strict;
use warnings;

my $identity = 'Joseph|Clark|May';
my $data_file = 'C:/tests/Profiles.txt';
open (DAT, $data_file) or die "$!";
OUTTER: while (<DAT>) {
   if (/^NAME $identity$/) {
      print; 
      while(<DAT>) {
         print;
         next OUTTER if (m|//|);
      } 
   }
}

close (DAT);

  #3 (permalink)  
Old 03-25-2009
Sp3ck Sp3ck is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 2
Thanx a million, KevinADC! That did it for me
Closed Thread

Bookmarks

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 09:08 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