in reply to Print multiple lines based on condition
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:
Invocation: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
Output zero.txt:c:\@Work\Perl\monks\syedasadali95>perl write_to_2_files_1.pl file_in.t +xt zero.txt one.txt
Output one.txt:1. command:read address:0xA 7. command:read address:0xC
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: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Print multiple lines based on condition
by syedasadali95 (Acolyte) on Mar 12, 2020 at 11:58 UTC | |
by AnomalousMonk (Archbishop) on Mar 12, 2020 at 19:47 UTC | |
by hippo (Archbishop) on Mar 12, 2020 at 13:46 UTC |