On Jun 21, 9:31 am, [EMAIL PROTECTED] (Dharshana Eswaran) wrote:
> Hi All,
>
> I am reading a certain data from one file and writing to another file. In
> the original file, there are few lines, which occur more than once in
> different lines. When i am writing it to the second file, i don't want it to
> be written more than once. I mean, it should not be repetitive. The file are
> just two simple text files.
>
> In achieving what i need, i thought simultaneous reading and writing to the
> file is required, to know if the lines are being written for the second
> time. But i don't know how to achieve this.
>
> A sample of the text file is shown below:
> STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
> {
>     STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
> protocol_discriminator;
>     STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;} 
> STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
> };
>
> STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T
> {
>     STACK_CC_SS_COMMON_TYPE_CHANNEL_TYPE_T channel_type;
>     STACK_CC_SS_COMMON_TYPE_CHANNEL_MODE_T channel_mode;} 
> STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T;
> };
>
> STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
> {
>     STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
> protocol_discriminator;
>     STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;
>
> } STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
> };
>

It looks as though your data is delimited by "};\n\n".  Is that
correct?  That is, each "block" of text ends with that string, and you
want to only print out the first instance of each block?  You can
control Perl's notion of what a "line" is via the $/ variable.  See
perldoc perlvar for more information.

To only print one instance of any given block, use a hash.  Each time
through your read loop, check to see if that block is in the hash.  If
it is, don't print it.  If it's not, do print it, and then add it to
the hash.  For example:

#!/usr/bin/perl
use strict;
use warnings;
open my $fh, '<', "infile.txt" or die $!;
open my $ofh, '>', "outfile.txt" or die $!;
local $/ = "};\n\n";
my %printed;
while (my $record = <$fh>) {
   print $ofh $record unless $printed{$record}++;
}
__END__

Hope that helps,
Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to