![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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);
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|