I don't really understand your requirements, but here's something that might serve to begin to narrow the range of possibilities. Note that I have used your example input file as is, including blank lines.

Source write_to_2_files_1.pl:

use strict; use warnings; use constant USAGE => <<"EOT"; usage: perl $0 file_in file_zero file_one where: file_in input file name file_zero output file name - address[0] bit == 0 file_one output file name - address[0] bit == 1 EOT die USAGE if @ARGV != 3; my ($file_in, $file_zero, $file_one) = @ARGV; open my $fh_in, '<', $file_in or die "opening '$file_in': $!"; open my $fh_0, '>', $file_zero or die "opening '$file_zero': $!"; open my $fh_1, '>', $file_one or die "opening '$file_one': $!"; my $fh_current; LINE: while (my $line = <$fh_in>) { next LINE unless $line =~ m{ \S }xms; # ignore blank lines my $got_command_addr = my ($rw_hex_addr) = $line =~ m{ \A \d+ [.] \s+ command: (?: read | write) \s+ address:0x ([[:xdigit:]]+) \s* \Z }xms; if ($got_command_addr) { $fh_current = 0x1 & hex $rw_hex_addr ? $fh_1 : $fh_0; } die "no command read/write address seen" unless $fh_current; print $fh_current $line; } close $fh_in or die "closing '$file_in': $!"; close $fh_0 or die "closing '$file_zero': $!"; close $fh_1 or die "closing '$file_one': $!"; exit; # subroutines ###################################################### # none for now
Invocation:
c:\@Work\Perl\monks\syedasadali95>perl write_to_2_files_1.pl file_in.t +xt zero.txt one.txt
Output zero.txt:
1. command:read address:0xA 7. command:read address:0xC
Output one.txt:
2. command:write address:0xB 3. writedata:0x12 4. writedata:0x34 5. writedata:0x56 6. writedata:0x78 8. command:write address:0xD 9. writedata:0x9A 10. writedata:0xBC 11. writedata:0xDE 12. writedata:0xF0

Update: Please note that it might have been helpful if you had provided expected output files for the OPed example input file. Also, please post input/output files and data, command lines and error messages as well as code within  <code> ... </code> tags. (Update: Please also see the update of this post by haukex which already touched on these points and included several very informative links.)


Give a man a fish:  <%-{-{-{-<


In reply to Re: Print multiple lines based on condition by AnomalousMonk
in thread Print multiple lines based on condition by syedasadali95

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.