This is a pretty classic example of where split and join come in handy.  Here I'm 
reading each line, splitting it into fields by the pipe character, then creating a new 
array with only those fields that begin with the letters I specify.  Then I use join 
to make a string with those fields I chose delimited by pipes again.  There may be 
more streamlined ways to do the rest of it, but this is the essence of why the split 
and join functions are there.

use strict;
use warnings;
open(INFILE,<myfile.txt")|| die "could not open file for reading!\n";
while(<INFILE>){
        chomp $_;
        my @fields = split(/\|/,$_);
        my @output;
        foreach my $field(@fields){
                if($field =~ /^(E|FE|NQ|IQ)/){
                        push @output,$field;
                }
        }
        print join('|',@output)."\n";
}

-----Original Message-----
From: Zary Necheva [mailto:[EMAIL PROTECTED]
Sent: Tuesday, November 25, 2003 12:45 PM
To: [EMAIL PROTECTED]
Subject: Help with extracting text file


Hi everyone,

I have a file with data similar to this
..........
Exxxxx|FExxxxx|NQxxxxxx|OUxxxxxx|GExxxxxx|OVxxxxxxx|IQxxxxxxxx|ORxxxx
Exxxxx|FExxxxxx|NQxxxxxx|GExxxxxxx|OVxxxxxx|OUxxxxxx|IQxxxxxxx|ORxxx 
Exxxxx|FExxxxxxx|NQxxxxxx|OUxxxxxx|OVxxxxxxx|ORxxxxxxx|IQxxxxxxx|RFxx
Exxxxx|FExxxxxx|NQxxxxxx|GExxxxxxx|OUxxxxxx|IQxxxxxxx|ORxxxxxxxx
.........
 
I would like to extract only these fields that start with E, FE, NQ, IQ 
and OU or to have this output

..........
Exxxxx|FExxxxx|NQxxxxxx|IQxxxxxx|OUxxxxxx
Exxxxx|FExxxxx|NQxxxxxx|IQxxxxxx|OUxxxxxx
Exxxxx|FExxxxx|NQxxxxxx|IQxxxxxx|OUxxxxxx
Exxxxx|FExxxxx|NQxxxxxx|IQxxxxxx|OUxxxxx
......

 Thanks,
Zary Necheva


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to