Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Print multiple lines based on condition

by AnomalousMonk (Bishop)
on Mar 12, 2020 at 19:47 UTC ( #11114185=note: print w/replies, xml ) Need Help??


in reply to Re^2: Print multiple lines based on condition
in thread Print multiple lines based on condition

Try this. Note that:

  • I'm using your example input and output files with blank lines already removed, but I think the code should work with blank lines in an input file (untested);
  • I'm comparing the generated output files to your example output files with the Windows fc (file compare) utility and there are no differences;
  • I'm using autodie to reduce the  ... or die ... noise, but this module only became part of core with Perl version 5.10, so you may have to put the noise back in;
  • This script is invoked in the same way as the previous one;
  • Tested under Perl version 5.8.9.
use strict; use warnings; use autodie; 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; my $rx_cmd_rw = qr{ cmd:SDP_CMD_ (?: RDBLKL | WRSIZEDFULL) }xms; open my $fh_in, '<', $file_in; open my $fh_0, '>', $file_zero; open my $fh_1, '>', $file_one; my $fh_current; LINE: while (my $line = <$fh_in>) { next LINE unless $line =~ m{ \S }xms; # ignore blank lines my $got_cmd_addr = my ($cmd_addr_in_hex) = $line =~ m{ $rx_cmd_rw .*? addr:0x ([[:xdigit:]]+) }xms; if ($got_cmd_addr) { $fh_current = 0x1 & hex $cmd_addr_in_hex ? $fh_1 : $fh_0; } die "no command read/write address seen" unless $fh_current; print $fh_current $line; } close $fh_in; close $fh_0; close $fh_1; exit; # subroutines ###################################################### # none for now


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11114185]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2020-05-29 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (166 votes). Check out past polls.

    Notices?