Title: RE: Create three different files based on search string in the i nput file

Fairly easy then. One last question to consider... Will the header/footer always be 2 lines (or some other static number)? If not then you may have to use pattern matching for those too (or ignore any lines that are not a match?). See below script.

BTW... you'll get more help from the list if you post in plain text. There are many on the list (not me) who hate posts in html or, at least, their mail clients do not interpolate html.

-Pete


# untested...

use strict;

my $datafile = "yourdatafilename.txt";
open(OUT1, ">> file1.txt") || die "Can't open file 1 : $1\n";
open(OUT2, ">> file2.txt") || die "Can't open file 2 : $1\n";
open(OUT3, ">> file3.txt") || die "Can't open file 3 : $1\n";

open(FILE,"< $datafile") || die "Can't open the file : $!\n";
foreach(<FILE>)
{
        my $out;
        if ($_ =~ /(abc|xyz|mlt)/)
        {
                &output(1,$_);
        }
        elsif ($_ =~ /(pqr|jkl)/)
        {
                &output(2,$_);
        }
        elsif ($_ =~ /(asd|frg|supp)/
        {
                &output(3,$_);
        }
        else
        {
                next;
        }

}

close(OUT1);
close(OUT2);
close(OUT3);

sub output
{
        my ($f,$data) = @_;
        print OUT1 "$data" if ($f == 1);
        print OUT2 "$data" if ($f == 2);
        print OUT3 "$data" if ($f == 3);
}




-----Original Message-----
From: AITHA, BHEEMSEN (SBCSI) [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, April 03, 2002 14:30
To: 'Peter Eisengrein'; [EMAIL PROTECTED]
Subject: RE: Create three different files based on search string in the i nput file


Here are the answers to the questions.

will the "small" file always have the same number of lines?  No.
is there are certain line number on which the match may appear?  No certain line number.
Or could it be anywhere other than header/footer? Yes.
is there a chance that it could match more than one?  Yes. Write it to each file for each match.

Thanx for the response.
-Bheemsen

-----Original Message-----
From: Peter Eisengrein [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, April 03, 2002 7:52 AM
To: AITHA, BHEEMSEN (SBCSI); [EMAIL PROTECTED]
Subject: RE: Create three different files based on search string in the i nput file


A couple questions:

- will the "small" file always have the same number of lines?
- is there are certain line number on which the match may appear? Or could it be anywhere other than header/footer?
- is there a chance that it could match more than one? If so, how do you want to handle it? Only write to the first file? Or write to each file for each match?

-----Original Message-----
From: AITHA, BHEEMSEN (SBCSI) [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, April 02, 2002 17:38
To: 'Peter Eisengrein'; [EMAIL PROTECTED]
Subject: Create three different files based on search string in the input file


Hi,
I have a small file. I want to read this file and create three different files based on a string found.
For example my small file is like this.

this is the header line
this too is header line
----
---
search strings are here abc xyz mlt
some more are here pqr jkl
some more are asd frg supp
----
---
this is footer
this is also footer...

Now the three different output files  should contain all the header and footer lines. But if "abc" or "xyz" or "mlt" is found on a line it should go to out file A only, if string "pqr" or "jkl" is found it should go to output file B only and if string "frg" or "supp" is found on a line it should go to out put file C only.

Can any one help me on how to do this easily using perl code.

Note: I am new to PERL.

Thanx..
-Bheemsen

Reply via email to